2016-07-25 103 views
-1
{"data": 
     { 
      "userId":"+919923911289", 
      "inTime":"2016-07-25 12:09:47+05:30", 
      "outTime":"0", 
      "totalTime":"0", 
      "type":"attendance" 
      } 
} 

而第二JSON響應是張貼下面

{"data":"please try again..."} 

我使用改造以獲得迴應。對於第一個json我創建了Gson模型。但是,當響應是第二個json格式時,我沒有任何響應模型。

我使用GsonModel爲第一個json響應顯示數據。 如果有第二種類型的迴應,我應該在這裏做什麼。我會在這裏使用TypeToken或其他東西。

它也給我解析例外,但我理解。但是,我不知道我必須做什麼。

+0

你得到錯誤的情況下,第二反應? – Blackbelt

+0

當我發佈正確的手機號碼。我會得到第一個響應,如果移動號碼不正確,或者與我的數據庫條目不匹配,它會給我第二個響應。 – ESHVAR289

回答

0
if(data.has("userId") && !data.isNull("userId")){ 
    String user_Id=data.getString("userId"); 
}else{ 
////show message "please try again..." here.... 
} 
+1

有關此代碼工作原因的一些說明會很有用 – hardillb

0
if (data.equalsIgnoreCase("please try again...") { 
    //show error 
} else { 
    //do your work with response 
} 
2

創建另一個模型

class ErrorModel{ 
    String data; 
} 

,並在你的主要方法:

try{ 
    GsonModel model = gson.fromJson(response,GsonModel.class); 
    // ... 
}catch(JsonSyntaxException e){ 
    ErrorModel model = gson.fromJson(response,ErrorModel.class); 
    // show error 
}