简体   繁体   中英

json parse error inside a Asyntask in android

I get this error "Sometimes" in my App, I use a Asyntask:

03-19 08:10:05.768: E/AndroidRuntime(280): FATAL EXCEPTION: main
03-19 08:10:05.768: E/AndroidRuntime(280): java.lang.NumberFormatException: unable to   parse 'null' as integer
 03-19 08:10:05.768: E/AndroidRuntime(280):     at   java.lang.Integer.parseInt(Integer.java:406)
 03-19 08:10:05.768: E/AndroidRuntime(280):     at java.lang.Integer.parseInt(Integer.java:382)
 03-19 08:10:05.768: E/AndroidRuntime(280):     at java.lang.Integer.valueOf(Integer.java:682)
 03-19 08:10:05.768: E/AndroidRuntime(280):     at Dic.proj.pkg.notifService$1$1$1$1.onPostExecute(notifService.java:76)
 03-19 08:10:05.768: E/AndroidRuntime(280):     at Dic.proj.pkg.notifService$1$1$1$1.onPostExecute(notifService.java:1)
 03-19 08:10:05.768: E/AndroidRuntime(280):     at android.os.AsyncTask.finish(AsyncTask.java:417)
03-19 08:10:05.768: E/AndroidRuntime(280):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-19 08:10:05.768: E/AndroidRuntime(280):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-19 08:10:05.768: E/AndroidRuntime(280):  at android.os.Handler.dispatchMessage(Handler.java:99)
 03-19 08:10:05.768: E/AndroidRuntime(280):     at android.os.Looper.loop(Looper.java:123)
 03-19 08:10:05.768: E/AndroidRuntime(280):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 03-19 08:10:05.768: E/AndroidRuntime(280):     at java.lang.reflect.Method.invokeNative(Native Method)
 03-19 08:10:05.768: E/AndroidRuntime(280):     at java.lang.reflect.Method.invoke(Method.java:521)
  03-19 08:10:05.768: E/AndroidRuntime(280):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  03-19 08:10:05.768: E/AndroidRuntime(280):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  03-19 08:10:05.768: E/AndroidRuntime(280):    at dalvik.system.NativeStart.main(Native Method)

i call my Asyntask inside a timer. this is my my Asyntask:

    // fetch new notifications
class fetch_last_asyn extends AsyncTask<Void, Integer, Boolean> {
    @Override
    protected Boolean doInBackground(Void... arg0) {
        RestClient client = new RestClient(
                "http://myaddress.org/api/get_last.php");
        client.AddParam("last", String.valueOf(last_notif_no));
        try {
            client.Execute(RequestMethod.GET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String response = client.getResponse();
        last_notifs = response.toString();
        return true;
    }
}

Inside my app I send my Asyntask result to parse to a code (parse_get_update), this is it:

    public static String[][] parse_get_update(String jsonResponse) {
    String[][] notifs = new String[3000][10];
    try {
        JSONArray jsonArray = new JSONArray(jsonResponse);
        for (int index = 0; index < jsonArray.length(); index++) {
            JSONObject json = jsonArray.getJSONObject(index);

            // get name & id here
            String id = json.getString("id");
            String notifno = json.getString("notifno");
            String title = json.getString("title");
            String description = json.getString("description");

            notifs[index][0] = id;
            notifs[index][1] = notifno;
            notifs[index][2] = title;
            notifs[index][3] = description;

            // notif_count++;
        }
        cnt2 = jsonArray.length();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return notifs;
}

Why do I get this error? What is NULL? I cant understand this.

I use ResClient for parse JSON. This error occures sometimes only, about 10% of times...

Thanks for your answers

The error means that you are trying to parse a string which has nothing in it. To demonstrate you have are doing like below.

String s = "";   
int i = Integer.parseInt(s);

You need to have a condition like below so that it doesn't parse if the string is empty. You can even use try catch block as well.

if(s is not null) {
  then do parse here
}
else {
  assign a default value
}

In your postExecute() method, somewhere, you're parsing a String to Integer using this - Integer.parseInt(str);

Most of the times that str is a valid number, hence, its working. But those 10% of times, that str is probably missing or coming as an emtpy String from the JSON and hence, a null is passed to the Integer.parseInt(null) , which is causing the java.lang.NumberFormatException .

Identify that piece of code, and put a null check for that, before parsing it to Integer .

发生这种情况是因为您正在将String解析为Integer看到此答案

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM