[英]parsing json inside an asyntask in android
I fetch information from a json source from web inside an android service, inside a timer loop. 我从计时器服务内的android服务中的Web获取json来源的信息。 some times, about 5% of times i get this error: 有时,大约有5%的时间出现此错误:
03-20 09:27:06.901: E/AndroidRuntime(401): FATAL EXCEPTION: main
03-20 09:27:06.901: E/AndroidRuntime(401): java.lang.NullPointerException
03-20 09:27:06.901: E/AndroidRuntime(401): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
03-20 09:27:06.901: E/AndroidRuntime(401): at org.json.JSONTokener.nextValue(JSONTokener.java:90)
03-20 09:27:06.901: E/AndroidRuntime(401): at org.json.JSONObject.<init>(JSONObject.java:154)
03-20 09:27:06.901: E/AndroidRuntime(401): at org.json.JSONObject.<init>(JSONObject.java:171)
03-20 09:27:06.901: E/AndroidRuntime(401): at Dic.proj.pkg.notifService.parse_if_update(notifService.java:182)
03-20 09:27:06.901: E/AndroidRuntime(401): at Dic.proj.pkg.notifService$1$1$1.onPostExecute(notifService.java:151)
03-20 09:27:06.901: E/AndroidRuntime(401): at Dic.proj.pkg.notifService$1$1$1.onPostExecute(notifService.java:1)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.os.AsyncTask.finish(AsyncTask.java:417)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.os.Looper.loop(Looper.java:123)
03-20 09:27:06.901: E/AndroidRuntime(401): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-20 09:27:06.901: E/AndroidRuntime(401): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 09:27:06.901: E/AndroidRuntime(401): at java.lang.reflect.Method.invoke(Method.java:521)
03-20 09:27:06.901: E/AndroidRuntime(401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-20 09:27:06.901: E/AndroidRuntime(401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-20 09:27:06.901: E/AndroidRuntime(401): at dalvik.system.NativeStart.main(Native Method)
i user RestClient libraryfot parsing json. 我使用RestClient libraryfot解析json。 and this is my parse_if_update Asyntask: 这是我的parse_if_update Asyntask:
public static String parse_if_update(String jsonResponse) {
String update = "";
try {
JSONObject json = new JSONObject(jsonResponse);
update = json.getString("update");
} catch (JSONException e) {
e.printStackTrace();
}
return update;
}
whats wrong with this? 这怎么了? i think it related to my asyntask because my asyntask runs several times sometimes... 我认为这与我的异步任务有关,因为有时我的异步任务会运行几次...
The NullPointerException
is thrown by the JSONTokener
, which makes me think that your jsonResponse
object is null
. NullPointerException
由JSONTokener
引发,这使我认为您的jsonResponse
对象为null
。 Add a check for this and you should be fine: 为此添加一张支票,您应该可以:
if (jsonResponse != null) {
JSONObject json = new JSONObject(jsonResponse);
update = json.getString("update");
}
else
update = "response was null!";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.