我正在尝试使用从网络服务器获取的一些坐标刷新地图。这是我的AsyncTask:
public class GetUsersAsyncTask extends AsyncTask<String, Void, String>
{
HttpResponse response = null;
InputStream is = null;
AndroidHttpClient httpclient;
String responseString = null;
@SuppressLint("NewApi")
@Override
protected String doInBackground(String... url) {
try{
Log.i("entrabackground",""+(++ae));
httpclient = AndroidHttpClient.newInstance("Android");
Log.i("!!","upa upa");
HttpUriRequest request = new HttpGet(mUrl+eventid);
Log.i("!!","taaa beemmm");
request.addHeader("Accept","application/json");
Log.i("!!","oh pahhh");
response = httpclient.execute(request);
} catch (ClientProtocolException e1) {
Log.i("erro1","Erro");
e1.printStackTrace();
} catch (IOException e1) {
Log.i("erro2","Erro");
e1.printStackTrace();
}
httpclient.close();
try {
if( String.valueOf(response.getEntity()) == "null")
{
responseString= "null";
Log.i("null", responseString.toString());
}
else
{
is = response.getEntity().getContent();
responseString = convertStreamToString(is);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("saibackgroud",""+(++ao));
return responseString;
}
@Override
protected void onPostExecute(String responseString) {
//super.onPostExecute(responseString);
//Do anything with response..
//Toast.makeText(getApplicationContext(), responseString,Toast.LENGTH_SHORT).show();
Log.i("entrapost",""+(++ai));
try
{
json = new JSONObject(responseString.trim());
//Toast.makeText(getApplicationContext(), json.toString(), Toast.LENGTH_SHORT).show();
}
catch (JSONException e) {
e.printStackTrace();
}
try{
Log.i("faz isto",""+(++au));
users_locations = json.getJSONArray("user");
userdraw.removeOverlay(item);
mapView.invalidate();
user_name=users_locations.getJSONObject(0).getString("name");
user_lat=users_locations.getJSONObject(0).getDouble("latitude");
user_lng=users_locations.getJSONObject(0).getDouble("longitude");
userpoint = new GeoPoint((int) (user_lat * 1E6), (int) (user_lng * 1E6));
item = new OverlayItem(userpoint,null,null);
//Get the new Drawable
//Set its bounds
usermarker.setBounds(-usermarker.getIntrinsicWidth()/2,- usermarker.getIntrinsicHeight(),usermarker.getIntrinsicWidth()/2,0);
//Set the new marker
item.setMarker(usermarker);
userdraw.addOverlay(item);
mc.animateTo(userpoint);
mapView.invalidate();
Log.i("userslocations",user_name+" "+user_lat+" "+user_lng);
}catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Connection Lost", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
从处理程序调用此asyncTask:
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
Log.i("url",mUrl+eventid);
/* do what you need to do */
//call asynctask
GetUsersAsyncTask getusers = new GetUsersAsyncTask();
getusers.execute(mUrl+eventid).toString();
/* and here comes the "trick" */
//10 seconds delay
handler.postDelayed(this, 30000);
}
};
runnable.run();
关于PostExecution任务的一些观点发生错误,我不知道为什么。它似乎在json = new JSONObject(responseString.trim());
这是我的LogCat:
01-16 00:44:37.005:E / AndroidRuntime(8625):未捕获的处理程序:由于未捕获的异常导致线程主要退出 01-16 00:44:37.105:E / AndroidRuntime(8625):java.lang.NullPointerException 01-16 00:44:37.105:E / AndroidRuntime(8625):at com.example.su2.EventMap $ GetUsersAsyncTask.onPostExecute(EventMap.java:273) 01-16 00:44:37.105:E / AndroidRuntime(8625):at com.example.su2.EventMap $ GetUsersAsyncTask.onPostExecute(EventMap.java:1) 01-16 00:44:37.105:E / AndroidRuntime(8625):在android.os.AsyncTask.finish(AsyncTask.java:417) 01-16 00:44:37.105:E / AndroidRuntime(8625):在android.os.AsyncTask.access $ 300(AsyncTask.java:127) 01-16 00:44:37.105:E / AndroidRuntime(8625):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:429) 01-16 00:44:37.105:E / AndroidRuntime(8625):在android.os.Handler.dispatchMessage(Handler.java:99) 01-16 00:44:37.105:E / AndroidRuntime(8625):在android.os.Looper.loop(Looper.java:123) 01-16 00:44:37.105:E / AndroidRuntime(8625):在android.app.ActivityThread.main(ActivityThread.java:4370) 01-16 00:44:37.105:E / AndroidRuntime(8625):at java.lang.reflect.Method.invokeNative(Native Method) 01-16 00:44:37.105:E / AndroidRuntime(8625):at java.lang.reflect.Method.invoke(Method.java:521) 01-16 00:44:37.105:E / AndroidRuntime(8625):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 01-16 00:44:37.105:E / AndroidRuntime(8625):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-16 00:44:37.105:E / AndroidRuntime(8625):at dalvik.system.NativeStart.main(Native Method) 01-16 00:44:37.285:E / SemcCheckin(8625):获取崩溃转储级别:java.io.FileNotFoundException:/ data / semc-checkin / crashdump
你能帮助我吗?
答案 0 :(得分:0)
您确定 responseString 不为空。
如果它是 null ,则调用 responseString.trim()将导致 java.lang.NullPointerException ,如上面的堆栈跟踪所示。