gpt4 book ai didi

Android Json 应用程序错误

转载 作者:行者123 更新时间:2023-11-30 03:54:48 24 4
gpt4 key购买 nike

我正在尝试使用 Json 和 MySQL 数据库创建一个 android 登录应用程序,但是当我运行它并尝试登录时,我收到以下错误:

11-22 12:29:08.079: E/AndroidRuntime(731): FATAL EXCEPTION: main
11-22 12:29:08.079: E/AndroidRuntime(731): java.lang.NullPointerException
11-22 12:29:08.079: E/AndroidRuntime(731): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
11-22 12:29:08.079: E/AndroidRuntime(731): at com.example.loctrans.DBConnection.DBConnection(DBConnection.java:41)
11-22 12:29:08.079: E/AndroidRuntime(731): at com.example.loctrans.MainActivity.onClick(MainActivity.java:56)
11-22 12:29:08.079: E/AndroidRuntime(731): at android.view.View.performClick(View.java:4084)
11-22 12:29:08.079: E/AndroidRuntime(731): at android.view.View$PerformClick.run(View.java:16966)
11-22 12:29:08.079: E/AndroidRuntime(731): at android.os.Handler.handleCallback(Handler.java:615)
11-22 12:29:08.079: E/AndroidRuntime(731): at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 12:29:08.079: E/AndroidRuntime(731): at android.os.Looper.loop(Looper.java:137)
11-22 12:29:08.079: E/AndroidRuntime(731): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-22 12:29:08.079: E/AndroidRuntime(731): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 12:29:08.079: E/AndroidRuntime(731): at java.lang.reflect.Method.invoke(Method.java:511)
11-22 12:29:08.079: E/AndroidRuntime(731): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-22 12:29:08.079: E/AndroidRuntime(731): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-22 12:29:08.079: E/AndroidRuntime(731): at dalvik.system.NativeStart.main(Native Method)

我在校验代码的时候,添加了一个log.e()方法来查找出错的行。这是我的 BD 连接代码

public class DBConnection extends Activity{
String result = null;
InputStream is = null;

public String DBConnection(ArrayList<NameValuePair> nameValuePairs, String URL){

try{
Log.e("info error", " fase 2");
HttpClient httpclient = new DefaultHttpClient();
Log.e("info error", " fase 3");
HttpPost httppost = new HttpPost(URL);
Log.e("info error", " fase 4");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.e("info error", httppost.toString());
HttpResponse response = httpclient.execute(httppost);
Log.e("info error", " fase 6");
HttpEntity entity = response.getEntity();
Log.e("info error", " fase 7");
is = entity.getContent();
Log.e("info error", " fase 8");
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Error in http connection"+e.toString(), Toast.LENGTH_LONG).show();
}
//convert response to string
try{
Log.e("info error", " fase 9");
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
Log.e("info error", " fase 10");
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.e("log_tag", "Cadena JSon " + result);
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Error converting result "+e.toString(),
Toast.LENGTH_LONG).show();
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}

public String getResult(){
return result;
}

}

在 logcat 中只有前三个 log.e 消息。

11-22 12:29:07.899: E/info error(731):  fase 2
11-22 12:29:07.919: E/info error(731): fase 3
11-22 12:29:07.929: E/info error(731): fase 4

所以我认为问题出在这一行: httppost.setEntity(新的 UrlEncodedFormEntity(nameValuePairs)但我不知道到底是什么错误

这是主要 Activity 代码的一部分:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Login" ,logi));
String URL = "http://192.168.1.10/Android/getlogin.php";
DBConnection DBC = new DBConnection();
String result = DBC.DBConnection(nameValuePairs, URL);

最佳答案

java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
at com.example.loctrans.DBConnection.DBConnection(DBConnection.java:41)

LogCat 报告问题出在您的 Toast 中的 getApplicationContext() 中,但这在 catch block 中。您是对的,真正的问题出在您的 Http 代码上,它可能会引发 NetworkOnMainThread 异常。 (您使用的是 Android 3.0+ 吗?)

您需要在不同的线程中执行可能较慢的网络操作。只需创建自定义 AsyncTask 并将您的代码移至 doInBackground()


DBConnection为什么要扩展Activity?您是否像未发布的代码中的 Activity 一样使用它?

关于Android Json 应用程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13518032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com