我試圖從我的數據庫中獲取數據,但它涉及到我的應用程序崩潰。 我已經試圖一步一步地調試,發現這條線會崩潰從MySQL/php獲取數據時出錯
jArray = new JSONArray(result);
這裏是我的全部代碼:
public class get_data extends AsyncTask<Void, Void, Boolean>
{
@Override
protected Boolean doInBackground(Void... params) {
String result = null;
InputStream is = null;
StringBuilder sb = null;
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/output.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
JSONArray jArray;
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
ct_name = new String[jArray.length()];
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
ct_id = json_data.getInt("A_ID");
ct_name[i] = json_data.getString("A_USERNAME");
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No output", Toast.LENGTH_LONG)
.show();
} catch (ParseException e1) {
e1.printStackTrace();
}
ausgabe.setText(ct_name[0]);
return null;
}
}
進出口調用它的onCreate:
新GET_DATA()執行();
報告:
11-22 12:31:18.762: E/AndroidRuntime(376): FATAL EXCEPTION: AsyncTask #1
11-22 12:31:18.762: E/AndroidRuntime(376): Process: com.JewE.engoneengine, PID: 376
11-22 12:31:18.762: E/AndroidRuntime(376): java.lang.RuntimeException: An error occured while executing doInBackground()
11-22 12:31:18.762: E/AndroidRuntime(376): at android.os.AsyncTask$3.done(AsyncTask.java:300)
11-22 12:31:18.762: E/AndroidRuntime(376): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-22 12:31:18.762: E/AndroidRuntime(376): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-22 12:31:18.762: E/AndroidRuntime(376): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-22 12:31:18.762: E/AndroidRuntime(376): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-22 12:31:18.762: E/AndroidRuntime(376): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-22 12:31:18.762: E/AndroidRuntime(376): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-22 12:31:18.762: E/AndroidRuntime(376): at java.lang.Thread.run(Thread.java:841)
11-22 12:31:18.762: E/AndroidRuntime(376): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-22 12:31:18.762: E/AndroidRuntime(376): at android.os.Handler.<init>(Handler.java:200)
11-22 12:31:18.762: E/AndroidRuntime(376): at android.os.Handler.<init>(Handler.java:114)
11-22 12:31:18.762: E/AndroidRuntime(376): at android.widget.Toast$TN.<init>(Toast.java:388)
11-22 12:31:18.762: E/AndroidRuntime(376): at android.widget.Toast.<init>(Toast.java:114)
11-22 12:31:18.762: E/AndroidRuntime(376): at android.widget.Toast.makeText(Toast.java:273)
11-22 12:31:18.762: E/AndroidRuntime(376): at com.JewE.engoneengine.java_get_data$get_data.doInBackground(java_get_data.java:90)
11-22 12:31:18.762: E/AndroidRuntime(376): at com.JewE.engoneengine.java_get_data$get_data.doInBackground(java_get_data.java:1)
11-22 12:31:18.762: E/AndroidRuntime(376): at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-22 12:31:18.762: E/AndroidRuntime(376): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-22 12:31:18.762: E/AndroidRuntime(376): ... 4 more
的PHP是正確的即時通訊讓我的JSON數據:
[{"A_ID":"1","A_USERNAME":"johann","A_PASSWORD":"joejoe"}]