error in parsing json inside an AsynTask in Android

不羁的心 提交于 2019-12-02 06:27:42

问题


I have an AsynTask and trying to retrieve a json and parse it, but I get this error:

03-21 11:38:07.033: E/AndroidRuntime(8439): FATAL EXCEPTION: main
03-21 11:38:07.033: E/AndroidRuntime(8439): java.lang.NullPointerException
03-21 11:38:07.033: E/AndroidRuntime(8439):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at org.json.JSONObject.<init>(JSONObject.java:154)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at org.json.JSONObject.<init>(JSONObject.java:171)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at Dic.proj.pkg.notifService.parse_if_update(notifService.java:191)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at Dic.proj.pkg.notifService$1$1$1.onPostExecute(notifService.java:153)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at Dic.proj.pkg.notifService$1$1$1.onPostExecute(notifService.java:1)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.os.AsyncTask.finish(AsyncTask.java:631)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.os.Looper.loop(Looper.java:137)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at android.app.ActivityThread.main(ActivityThread.java:4921)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at java.lang.reflect.Method.invoke(Method.java:511)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
03-21 11:38:07.033: E/AndroidRuntime(8439):     at dalvik.system.NativeStart.main(Native Method)

and this is my parse_if_update function:

public static String parse_if_update(String jsonResponse) {
    String update="no";

    try {
        JSONObject json = new JSONObject(jsonResponse);
        update = json.getString("update");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    if(update == null){
        update="no";
    }
    return update;
}

What's wrong? I spend two days to solve this problem. Sometimes I encounter null values but I don't know how to treat with them.


回答1:


Add simple different from null tests.

public static String parse_if_update(String jsonResponse) {
String update="no";

    if (jsonResponse != null) {    
        try {
                JSONObject json = new JSONObject(jsonResponse);
                if (json != null) { 
                    update = json.getString("update");
                }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(update == null){
            update="no";
        }
    }
    return update;
}

Do you still have the problem then ?




回答2:


Below code will work:

 public static String parse_if_update(String jsonResponse) {
        String update="no";

        try {
                JSONObject json = new JSONObject(jsonResponse);
                if(json!=null)
                   update = json.getString("update");
                else
                   update = "no";
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(update == null){
            update="no";
        }
        return update;
    }



回答3:


Check if the String "update" is null with:

if (!json.isNull("update")) {
    update = json.getString("update");
}

If you dont check this, it can raise a nullpointer



来源:https://stackoverflow.com/questions/15542404/error-in-parsing-json-inside-an-asyntask-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!