1

我在获取 json(空值)时收到此错误:

03-21 12:32:02.918: E/AndroidRuntime(20383): FATAL EXCEPTION: AsyncTask #2
03-21 12:32:02.918: E/AndroidRuntime(20383): java.lang.RuntimeException: An error occured while executing doInBackground()
03-21 12:32:02.918: E/AndroidRuntime(20383):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.lang.Thread.run(Thread.java:856)
03-21 12:32:02.918: E/AndroidRuntime(20383): Caused by: java.lang.NullPointerException
03-21 12:32:02.918: E/AndroidRuntime(20383):    at Dic.proj.pkg.notifService$check_last_asyn.doInBackground(notifService.java:253)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at Dic.proj.pkg.notifService$check_last_asyn.doInBackground(notifService.java:1)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-21 12:32:02.918: E/AndroidRuntime(20383):    ... 4 more

这是我的 check_last_asyn AsynTask:

// check if notification is available asyntask
class check_last_asyn extends AsyncTask<Void, Integer, Boolean> {
    @Override
    protected Boolean doInBackground(Void... arg0) {

        if(isOnline()){
        RestClient client = new RestClient(
                "http://mysite.org/api/check_last.php");
        client.AddParam("last", String.valueOf(last_notif_no));
        try {
            client.Execute(RequestMethod.GET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String response = client.getResponse();
        if(!response.equals(null)){
            update_response = response;
        }else{
            update_response= "{'update':'no'}";
        }
        }
        return true;
    }
}

我知道这是因为空值,但我不知道如何在这段代码中控制空值...

4

4 回答 4

1

检查应该是这样的:

if(response != null && !response.equals(""))
于 2013-03-21T09:15:48.650 回答
1

这应该做

if(response!=null && !(response.trim()).equals("")){
    update_response = response;
}else{
    update_response= "{'update':'no'}";
}
于 2013-03-21T09:18:00.870 回答
1

我认为,String responcevar 可能为空,所以只需添加对空值的检查:

String responce = client.getResponce();

if (responce == null) {
    // As flag, which represents, that there is no answer from service
    // Or other logic
    return false; 
}

// your code
于 2013-03-21T09:18:57.767 回答
1

你可以控制喜欢

if(!TextUtils.isEmpty(response)){
        try {
                JSONObject  jobj =  new JSONObject(response);               
                String stolan_id = null;
                Boolean bstolan_id = jobj.has("keyhere");
                if(bstolan_id){             
                    stolan_id = jobj.getString("keyhere"); 
                }
       } catch (JSONException e) {
                e.printStackTrace();
            }

    }
于 2013-03-21T09:19:13.803 回答