我从服务器端收到具有相同“键"的两个json响应. [英] I having the two json response from the server side with the same "key".The json is posted below
问题描述
{"data":
{
"userId":"+919923911289",
"inTime":"2016-07-25 12:09:47+05:30",
"outTime":"0",
"totalTime":"0",
"type":"attendance"
}
}
第二个json响应是
And the second json response is
{"data":"please try again..."}
我正在使用改造来获得响应.我为第一个json创建了Gson模型.但是,当响应为第二个json格式时,我没有任何响应模型.
I am using retrofit to get the response . For the first json i have created the Gson model.But , when the response is in the second json format i didn't have any response model for it.
我正在使用GsonModel来显示第一个json响应的数据. 如果有第二种类型的响应,我应该在这里做什么.我将在这里使用TypeToken还是其他方式.
I am displaying the data using the GsonModel for the first json response. And if there is second type of response what should i do here.Will i use the TypeToken here or something else.
这也给了我我所理解的解析异常.但是,我不知道该做什么.
And it also giving me the parsing exception wich i understand.But , i didn't know what i have to do.
推荐答案
创建另一个模型
class ErrorModel{
String data;
}
以及您的主要方法中:
try{
GsonModel model = gson.fromJson(response,GsonModel.class);
// ...
}catch(JsonSyntaxException e){
ErrorModel model = gson.fromJson(response,ErrorModel.class);
// show error
}
这篇关于我从服务器端收到具有相同“键"的两个json响应.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!