早上好StackOver Flow Pros,
我需要一点帮助。我正在运行一个通过php脚本调用MySql数据库的Android应用程序,当我运行应用程序时,我得到一个由空指针异常引起的AsyncTask#1的致命异常错误。它说它在执行doInBackground时发生了。这是我从MyBringBack关注的教程。我真的需要围绕这个问题,因为我一直试图解决这个问题。如果我删除/评论大多数Log.d语句它会运行正常,但我不会看到JSON响应中发生的事情的标签,下面列出了代码,任何人都可以帮我解决这个问题或者告诉我什么代码有问题。
以下是来自logcat的错误日志
10-25 09:27:47.818:E / JSON Parser(1041):解析数据时出错org.json.JSONException:java.lang.String类型的值数组(2)无法转换为JSONObject 10-25 09:27:47.818:W / dalvikvm(1041):threadid = 11:线程退出未捕获异常(组= 0xb1b06ba8) 10-25 09:27:47.828:E / AndroidRuntime(1041):致命异常:AsyncTask#1 10-25 09:27:47.828:E / AndroidRuntime(1041):进程:com.bobcatservice,PID:1041 10-25 09:27:47.828:E / AndroidRuntime(1041):java.lang.RuntimeException:执行doInBackground()时发生错误 10-25 09:27:47.828:E / AndroidRuntime(1041):在android.os.AsyncTask $ 3.done(AsyncTask.java:300) 10-25 09:27:47.828:E / AndroidRuntime(1041):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 10-25 09:27:47.828:E / AndroidRuntime(1041):at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 10-25 09:27:47.828:E / AndroidRuntime(1041):at java.util.concurrent.FutureTask.run(FutureTask.java:242) 10-25 09:27:47.828:E / AndroidRuntime(1041):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 10-25 09:27:47.828:E / AndroidRuntime(1041):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 10-25 09:27:47.828:E / AndroidRuntime(1041):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 10-25 09:27:47.828:E / AndroidRuntime(1041):at java.lang.Thread.run(Thread.java:841) 10-25 09:27:47.828:E / AndroidRuntime(1041):引起:java.lang.NullPointerException 10-25 09:27:47.828:E / AndroidRuntime(1041):at com.bobcatservice.Login $ AttemptLogin.doInBackground(Login.java:126) 10-25 09:27:47.828:E / AndroidRuntime(1041):at com.bobcatservice.Login $ AttemptLogin.doInBackground(Login.java:1) 10-25 09:27:47.828:E / AndroidRuntime(1041):在android.os.AsyncTask $ 2.call(AsyncTask.java:288) 10-25 09:27:47.828:E / AndroidRuntime(1041):at java.util.concurrent.FutureTask.run(FutureTask.java:237) 10-25 09:27:47.828:E / AndroidRuntime(1041):......还有4个 10-25 09:27:49.968:E / WindowManager(1041):android.view.WindowLeaked:Activity com.bobcatservice.Login泄露了窗口com.android.internal.policy.impl.PhoneWindow $ DecorView {b1e8e430 VE ...最初添加的R ...... D 0,0-684,192} 10-25 09:27:49.968:E / WindowManager(1041):在android.view.ViewRootImpl。(ViewRootImpl.java:348) 10-25 09:27:49.968:E / WindowManager(1041):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248) 10-25 09:27:49.968:E / WindowManager(1041):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 10-25 09:27:49.968:E / WindowManager(1041):在android.app.Dialog.show(Dialog.java:286)
这是有问题的代码
//AsyncTask is a seperate thread than the thread that runs the GUI
//Any type of networking should be done with asynctask.
class AttemptLogin extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
try {
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, Welcome.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
答案 0 :(得分:3)
如果我删除/评论大多数Log.d语句,它将运行良好
似乎我们可以在doInBackground()
方法中的某些日志调用中找到问题。这个如何?
Log.d("request!", "starting");
不,似乎很好。下一个呢?
Log.d("Login attempt", json.toString());
啊哈!我们如何获得此json
变量?
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
检查您的http请求的结果,您可能会在此处获得null
。
如果json
不为空,该怎么办?好吧,我们还有另一个可疑的日志调用:
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
据我记忆,如果Log.d()
的第二个参数是null
,那么您将获得NullPointerException
,所以请检查一下。
如果它不是问题怎么办?好吧,我建议你use debugger来这里。
答案 1 :(得分:1)
在你的代码之上pls检查create_product.php是否存在(在android文件夹中)并用你的表名替换table_name并make $ response [“message”] = mysql_error();在android sdk的logcat中知道create_product.php有什么问题
// url to create send data. This contains the ip address of my machine on which the local server is running. You will write the IP address of your machine
private static String url = "http://192.168.***.1:8080/android/create_product.php";
create_product.php
<?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['id']) && isset($_POST['name'])) {
$id = $_POST['id'];
$name = $_POST['name'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO table_name(id, name) VALUES('$id', '$name')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = mysql_error();;
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
并且还添加清单此代码,否则此错误将生成
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />