2016-12-29 32 views
1

我有一個JSON效應初探,像這樣(在下面給出)的JSONObject響應比較

{ 
    "result": "NO RECORD FOUND" 
} 

和我有這樣

okhttp3.Response response = client.newCall(request).execute(); 
jsonObject = new JSONObject(response.body().string()); 

if(jsonObject != null) { 
    name = jsonObject.getJSONObject("result").getString("name") 
    dlObj.put("dlName", name); 
} 

的條件的響應將執行一次他們將在一些數據JSONObject,但我想要的情況下,當JSONObject的響應像沒有記錄找到,那麼它不應該進入條件檢查機構,他們反正使用哪個我可以比較上述案例?

回答

1

您的意思是響應代碼404? 如果是的話對證404碼的響應

if (response.code()==404){ 

// deal with it 
} 

否則,如果你僅僅意味着JSON對象

if(jsonObject != null) { 

    result = jsonObject.getString("result") 
    if(result.equals("NO RECORD FOUND"){ 
     //deal with it 
    }else{ 
     //have fun 
    } 
} 

的結果,財產,如果你不知道結果屬性存在使用hasString檢查它之前試圖獲得它。

+0

非常感謝它的工作 – Mavericks