【问题标题】:MySQL - Android App Connection with JSON (with logcat)MySQL - 使用 JSON 的 Android 应用程序连接(使用 logcat)
【发布时间】:2013-02-18 19:29:41
【问题描述】:

我正在尝试从我的 MySQL-DB 中获取密码,但 android 程序无法启动, 这是我的 MainActivity.java 代码:

package com.example.datenbankconnectiontest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends ListActivity {

JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
     HttpClient httpclient = new DefaultHttpClient();
     HttpPost httppost = new HttpPost("http://178.190.156.114/Datenbankconnectiontest.php");
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     HttpResponse response = httpclient.execute(httppost);
     HttpEntity entity = response.getEntity();                                                          
     is = entity.getContent();
     }catch(Exception e){
         Log.e("log_tag", "Error in http connection"+e.toString());
    }
//convert response to string
try{
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
       sb = new StringBuilder();
       sb.append(reader.readLine() + "\n");

       String line="0";
       while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
        }catch(Exception e){
              Log.e("log_tag", "Error converting result "+e.toString());
        }
//paring data
int ct_id;
String ct_name;
try{
      jArray = new JSONArray(result);
      JSONObject json_data=null;
      for(int i=0;i<jArray.length();i++){
             json_data = jArray.getJSONObject(i);
             ct_id=json_data.getInt("CITY_ID");
             ct_name=json_data.getString("CITY_NAME");
             Toast.makeText(getBaseContext(), "PW: " +ct_name ,Toast.LENGTH_LONG).show();
         }

      }
      catch(JSONException e1){
          Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
      } catch (ParseException e1) {
            e1.printStackTrace();
    }
}
}

这是我的日志:

02-18 19:21:42.831: E/Trace(675): error opening trace file: No such file or directory (2)
02-18 19:21:43.331: D/AndroidRuntime(675): Shutting down VM
02-18 19:21:43.331: W/dalvikvm(675): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-18 19:21:43.340: E/AndroidRuntime(675): FATAL EXCEPTION: main
02-18 19:21:43.340: E/AndroidRuntime(675): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datenbankconnectiontest/com.example.datenbankconnectiontest.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.os.Looper.loop(Looper.java:137)
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.app.ActivityThread.main(ActivityThread.java:4745)
02-18 19:21:43.340: E/AndroidRuntime(675):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 19:21:43.340: E/AndroidRuntime(675):  at java.lang.reflect.Method.invoke(Method.java:511)
02-18 19:21:43.340: E/AndroidRuntime(675):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-18 19:21:43.340: E/AndroidRuntime(675):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-18 19:21:43.340: E/AndroidRuntime(675):  at dalvik.system.NativeStart.main(Native Method)
02-18 19:21:43.340: E/AndroidRuntime(675): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
02-18 19:21:43.340: E/AndroidRuntime(675):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:259)
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.app.Activity.setContentView(Activity.java:1867)
02-18 19:21:43.340: E/AndroidRuntime(675):  at com.example.datenbankconnectiontest.MainActivity.onCreate(MainActivity.java:35)
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.app.Activity.performCreate(Activity.java:5008)
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-18 19:21:43.340: E/AndroidRuntime(675):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-18 19:21:43.340: E/AndroidRuntime(675):  ... 11 more

如果您能帮我找出错误,我将非常感激!如果您需要 PHP 端,请告诉我,但我认为没有必要,虽然它看起来像一个 android 错误。

代码不是我自己写的,是从http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php/复制过来的(变量和url也改了)

新的堆栈跟踪:

02-18 19:38:42.040: E/Trace(731): error opening trace file: No such file or directory (2)
02-18 19:38:42.580: E/log_tag(731): Error in http connectionandroid.os.NetworkOnMainThreadException
02-18 19:38:42.580: E/log_tag(731): Error converting result java.lang.NullPointerException
02-18 19:38:42.580: D/AndroidRuntime(731): Shutting down VM
02-18 19:38:42.580: W/dalvikvm(731): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-18 19:38:42.600: E/AndroidRuntime(731): FATAL EXCEPTION: main
02-18 19:38:42.600: E/AndroidRuntime(731): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datenbankconnectiontest/com.example.datenbankconnectiontest.MainActivity}: java.lang.NullPointerException
02-18 19:38:42.600: E/AndroidRuntime(731):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-18 19:38:42.600: E/AndroidRuntime(731):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-18 19:38:42.600: E/AndroidRuntime(731):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-18 19:38:42.600: E/AndroidRuntime(731):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-18 19:38:42.600: E/AndroidRuntime(731):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 19:38:42.600: E/AndroidRuntime(731):  at android.os.Looper.loop(Looper.java:137)
02-18 19:38:42.600: E/AndroidRuntime(731):  at android.app.ActivityThread.main(ActivityThread.java:4745)
02-18 19:38:42.600: E/AndroidRuntime(731):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 19:38:42.600: E/AndroidRuntime(731):  at java.lang.reflect.Method.invoke(Method.java:511)
02-18 19:38:42.600: E/AndroidRuntime(731):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-18 19:38:42.600: E/AndroidRuntime(731):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-18 19:38:42.600: E/AndroidRuntime(731):  at dalvik.system.NativeStart.main(Native Method)
02-18 19:38:42.600: E/AndroidRuntime(731): Caused by: java.lang.NullPointerException
02-18 19:38:42.600: E/AndroidRuntime(731):  at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
02-18 19:38:42.600: E/AndroidRuntime(731):  at org.json.JSONTokener.nextValue(JSONTokener.java:94)
02-18 19:38:42.600: E/AndroidRuntime(731):  at org.json.JSONArray.<init>(JSONArray.java:87)
02-18 19:38:42.600: E/AndroidRuntime(731):  at org.json.JSONArray.<init>(JSONArray.java:103)
02-18 19:38:42.600: E/AndroidRuntime(731):  at com.example.datenbankconnectiontest.MainActivity.onCreate(MainActivity.java:68)
02-18 19:38:42.600: E/AndroidRuntime(731):  at android.app.Activity.performCreate(Activity.java:5008)
02-18 19:38:42.600: E/AndroidRuntime(731):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-18 19:38:42.600: E/AndroidRuntime(731):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-18 19:38:42.600: E/AndroidRuntime(731):  ... 11 more

【问题讨论】:

    标签: android mysql eclipse logcat


    【解决方案1】:

    阻止您的应用启动的当前问题是您的activity_main 布局需要有一个 ID 为 @android:id/list 的 ListView,例如

    <ListView 
         android:id="@android:id/list"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
    </ListView>
    

    因为堆栈跟踪说:

    RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
    

    【讨论】:

    • 谢谢,这个(迟钝,我自己没有得到它:P)是固定的,其他的仍然是开放的:S
    • @Werdli 是的,小错误通常很难发现 :-) 此外,如果您的应用程序仍然崩溃,那么您应该发布更新的堆栈跟踪。但是,不要破坏上一个问题,以免丢失我的答案的上下文。
    • 嗯,好像result 是你假冒的(如果第68 行是jArray = new JSONArray(result);)是null,所以它不能正确转换。此外,当您在新版本的 Android 上运行代码时,您将获得NetworkOnMainThreadExceptions,因为您在主线程中执行网络操作。这就是运行后的下一步。
    • 我实际上不知道为什么结果可能为空(如上所述,我对 android 比较陌生,只是复制了代码)。当我打开 .php 文件时,我会显示我想输入结果的密码。对于这个问题,我能用这种“低”知识做些什么吗?另外,如果没有,您是否有任何用于 mysql-android 连接的下载源代码网站?对不起,你花了这么多时间,成为一个新手:S
    • @Werdli 您可以制作多个打印语句,请检查变量的值。另外,既然你确实说你是新人,在 Android/Java 领域,我建议关注thisthis。不幸的是,我帮不上忙,而且我对 PHP 和 JSON 都不熟悉。我遇到的第一个链接是this,这可能会有所帮助。
    猜你喜欢
    • 2017-10-29
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    相关资源
    最近更新 更多