2016-07-25 101 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 
}