我正在使用JSONParser创建Android应用程序以从Web服务器获取数据并使用列表视图显示此数据,而不是使用 onListItemClick 方法在具有某些详细信息的新活动中显示所选数据单击按钮加载数据并在日志Cat
中显示后系统强制03-11 09:35:25.185: E/AndroidRuntime(2439): FATAL EXCEPTION: AsyncTask #1
03-11 09:35:25.185: E/AndroidRuntime(2439): java.lang.RuntimeException: An error occured while executing doInBackground()
03-11 09:35:25.185: E/AndroidRuntime(2439): at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-11 09:35:25.185: E/AndroidRuntime(2439): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
03-11 09:35:25.185: E/AndroidRuntime(2439): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
03-11 09:35:25.185: E/AndroidRuntime(2439): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
03-11 09:35:25.185: E/AndroidRuntime(2439): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-11 09:35:25.185: E/AndroidRuntime(2439): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-11 09:35:25.185: E/AndroidRuntime(2439): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-11 09:35:25.185: E/AndroidRuntime(2439): at java.lang.Thread.run(Thread.java:841)
03-11 09:35:25.185: E/AndroidRuntime(2439): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-11 09:35:25.185: E/AndroidRuntime(2439): at android.os.Handler.<init>(Handler.java:197)
03-11 09:35:25.185: E/AndroidRuntime(2439): at android.os.Handler.<init>(Handler.java:111)
03-11 09:35:25.185: E/AndroidRuntime(2439): at android.app.Activity.<init>(Activity.java:759)
03-11 09:35:25.185: E/AndroidRuntime(2439): at android.app.ListActivity.<init>(ListActivity.java:175)
03-11 09:35:25.185: E/AndroidRuntime(2439): at com.devleb.jsonparsingactivitydemo.JSONParserHandler.<init>(JSONParserHandler.java:23)
03-11 09:35:25.185: E/AndroidRuntime(2439): at com.devleb.jsonparsingactivitydemo.JsonActivityHttpClient$HTTPGetTask.doInBackground(JsonActivityHttpClient.java:44)
03-11 09:35:25.185: E/AndroidRuntime(2439): at com.devleb.jsonparsingactivitydemo.JsonActivityHttpClient$HTTPGetTask.doInBackground(JsonActivityHttpClient.java:1)
03-11 09:35:25.185: E/AndroidRuntime(2439): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-11 09:35:25.185: E/AndroidRuntime(2439): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-11 09:35:25.185: E/AndroidRuntime(2439): ... 4 more
如果有人能帮助我,我将不胜感激。
package com.devleb.jsonparsingactivitydemo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.app.ListActivity;
import android.content.Intent;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class JsonActivityHttpClient extends ListActivity {
// ******variables for the inner class*********//
private static List<String> result;
private JSONObject postalCode = null;
String SplaceName, Slng, Slat, SpostalCode;
private static final String PLACE_NAME_TAG = "placeName";
private static final String LONGITUDE_TAG = "lng";
private static final String LATITUDE_TAG = "lat";
private static final String POSTAL_CODE_TAG = "postalcode";
private static final String POSTALCODE = "postalcodes";
// ******variables for the inner class*********//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new HTTPGetTask().execute();
}
private class HTTPGetTask extends AsyncTask<Void, Void, List<String>> {
private static final String USER_NAME = "devleb";
private static final String URL = "http://api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username="
+ USER_NAME;
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");
@Override
protected List<String> doInBackground(Void... arg0) {
// TODO Auto-generated method stub
HttpGet request = new HttpGet(URL);
JSONParserHandler responseHandler = new JSONParserHandler();
try {
return mClient.execute(request, responseHandler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<String> result) {
// TODO Auto-generated method stub
if (null != mClient) {
mClient.close();
setListAdapter(new ArrayAdapter<String>(
JsonActivityHttpClient.this, R.layout.list_item, result));
}
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
try {
SplaceName = postalCode.get(PLACE_NAME_TAG).toString();
Slat = postalCode.getString(LATITUDE_TAG).toString();
Slng = postalCode.getString(LONGITUDE_TAG).toString();
SpostalCode = postalCode.getString(POSTAL_CODE_TAG).toString();
} catch (Exception e) {
Log.e("ASSAIGN JSON node to String", "ERROR in Assaign!!");
}
Intent in = new Intent(getBaseContext(), RowItem.class);
in.putExtra(PLACE_NAME_TAG, SplaceName);
in.putExtra(LATITUDE_TAG, Slat);
in.putExtra(LONGITUDE_TAG, Slng);
in.putExtra(POSTAL_CODE_TAG, SpostalCode);
startActivity(in);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.json_activity_http_client, menu);
return true;
}
// ***************INNER CLASS***********************************//
private class JSONParserHandler implements ResponseHandler<List<String>> {
// String SplaceName, Slng, Slat, SpostalCode;
// public JSONObject postalCode = null;
// public static List<String> result;
/*
* public static final String PLACE_NAME_TAG = "placeName"; private
* static final String LONGITUDE_TAG = "lng"; private static final
* String LATITUDE_TAG = "lat";
*
* private static final String POSTAL_CODE_TAG = "postalcode"; private
* static final String POSTALCODE = "postalcodes";
*/
@Override
public List<String> handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
result = new ArrayList<String>();
String JSONResponse = new BasicResponseHandler()
.handleResponse(response);
try {
JSONObject jsonObject = (JSONObject) new JSONTokener(
JSONResponse).nextValue();
JSONArray PostalCodes = jsonObject.getJSONArray(POSTALCODE);
for (int i = 0; i < PostalCodes.length(); i++) {
postalCode = (JSONObject) PostalCodes.get(i);
result.add(postalCode.get(PLACE_NAME_TAG) + "\n"
+ LATITUDE_TAG + ":" + postalCode.get(LATITUDE_TAG)
+ "," + LONGITUDE_TAG + ":"
+ postalCode.get(LONGITUDE_TAG) + "\n"
+ POSTAL_CODE_TAG + ":"
+ postalCode.get(POSTAL_CODE_TAG)
);
}
} catch (JSONException E) {
E.printStackTrace();
}
return result;
}
}
// ***************INNER CLASS***********************************//
}
答案 0 :(得分:1)
JsonParseHandler
实际上是您的Activity
。你主要显示。
首先,您不应该初始化它。它是Activity
,应该以{{1}}来电启动。
我认为你应该这样做。
startActivity()
应该只是解析JSON。它不应该扩展ListActivity。你应该将onListItemClick函数移动到JsonParseHandler
答案 1 :(得分:0)
从您的logcat异常我可以说,您正在尝试从外部线程创建处理程序,这在android中是不允许的。
答案 2 :(得分:0)
如果你想在doinbackground方法中创建新的句柄,请试试这个。
runOnUiThread(new Runnable() {
public void run() {
// your handler
}
});