Java 在android中获取json时控制空值

Java 在android中获取json时控制空值,java,android,android-asynctask,nullpointerexception,rest-client,Java,Android,Android Asynctask,Nullpointerexception,Rest Client,我在获取json时遇到此错误(空值): 这是我最后一张支票 // check if notification is available asyntask class check_last_asyn extends AsyncTask<Void, Integer, Boolean> { @Override protected Boolean doInBackground(Void... arg0) { if(isOnline()){ R

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

这是我最后一张支票

// 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;
    }
}
//检查通知是否在任务中可用
类检查\u last\u asyn任务{
@凌驾
受保护的布尔doInBackground(无效…arg0){
if(isOnline()){
RestClient=newrestclient(
"http://mysite.org/api/check_last.php");
client.AddParam(“last”,String.valueOf(last_notif_no));
试一试{
client.Execute(RequestMethod.GET);
}捕获(例外e){
e、 printStackTrace();
}
字符串响应=client.getResponse();
如果(!response.equals(null)){
更新_响应=响应;
}否则{
update_response=“{'update':'no'}”;
}
}
返回true;
}
}

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

检查应类似于:

if(response != null && !response.equals(""))

支票应类似于:

if(response != null && !response.equals(""))
这应该可以

if(response!=null && !(response.trim()).equals("")){
    update_response = response;
}else{
    update_response= "{'update':'no'}";
}
这应该可以

if(response!=null && !(response.trim()).equals("")){
    update_response = response;
}else{
    update_response= "{'update':'no'}";
}

我认为,
String responce
var可能为null,所以只需添加null值检查:

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

我认为,
String responce
var可能为null,所以只需添加null值检查:

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
你可以控制自己

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

    }
你可以控制自己

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

    }

@艾米拉兹如果你仔细看,那就不一样了。不是部分答案。@EmilAdz,甚至在我的答案是全面的之前。@EmilAdz,如果你仔细看,它就不同了。不是部分答案。@EmilAdz甚至在我的答案是全面的之前。你肯定:
last\u notif\u no
from:
client.AddParam(“last”,String.valueOf(last\u notif\u no))不为空?请参考此项是,它始终具有值您是否确定:
last\u notif\u no
from:
client.AddParam(“last”,String.valueOf(last\u notif\u no))不为空?请参考此项是,它始终具有值