我试图从我的数据库中获取数据,但是我的应用程序崩溃了。 我已经尝试逐步调试它,发现这一行将崩溃
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中调用它:
new get_data()。execute();
报告:
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"}]
答案 0 :(得分:1)
使用下面显示的异步任务,不要忘记在Android清单中包含权限
<uses-permission android:name="android.permission.INTERNET" />
将此用于Android中的异步任务:
调用异步任务从任何方法使用此方法:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("key1", "value1"));
params.add(new BasicNameValuePair("key1", "value2"));
new WEBSERVICEREQUESTOR(URL, params).execute();
将此作为班级成员:
class WEBSERVICEREQUESTOR extends AsyncTask<String, Integer, String>
{
String URL;
List<NameValuePair> parameters;
private ProgressDialog pDialog;
public WEBSERVICEREQUESTOR(String url, List<NameValuePair> params)
{
this.URL = url;
this.parameters = params;
}
@Override
protected void onPreExecute()
{
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Processing Request...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params)
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
HttpPost httpPost = new HttpPost(URL);
if (parameters != null)
{
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
}
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity);
} catch (Exception e)
{
}
return "";
}
@Override
protected void onPostExecute(String result)
{
pDialog.dismiss();
try
{
} catch (Exception e)
{
}
super.onPostExecute(result);
}
}
答案 1 :(得分:0)
您可以尝试执行以下操作,而不是直接构造和调用AsyncTask:
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void Run() {
new get_data().execute();
}
});
答案 2 :(得分:0)
我明白了!
我只是忘了删除我的php脚本中的html标签:)现在我就是这样的:
<?php
...
?>
就是这样!希望我能帮助别人