【发布时间】:2017-04-10 11:39:36
【问题描述】:
如何使用同步请求处理 Retrofit 2 的错误响应?
我需要在正常情况下返回宠物数组的过程响应,如果请求的参数错误,则返回错误 json 对象。这两种情况如何处理?
我正在尝试使用this 教程,但主要问题是将正常和错误 json 映射到对象。
我的正常反应示例:
[ {
"type" : "cat",
"color": "black"
},
{
"type" : "cat",
"color": "white"
} ]
错误响应示例:
{"error" = "-1", error_description = "Low version"}
我得到了什么:
Call<List<Pet>> call = getApiService().getPet(1);
Response<List<Pet>> response;
List<Pet> result = null;
try {
response = call.execute(); //line with exception "Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path"
if(!response.isSuccessful()){
Error error = parseError(response);
Log.d("error message", error.getErrorDescription());
}
if (response.code() == 200) {
result = response.body();
}
} catch (IOException e) {
e.printStackTrace();
}
【问题讨论】:
-
你的代码到底有什么问题?