error in parsing json inside an AsynTask in Android

人走茶凉 提交于 2019-12-01 23:49:51

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 ?

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;
    }

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

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