如何使用同步请求处理 Retrofit 2 的错误响应?
我需要处理响应,在正常情况下返回 pets 数组,如果请求有错误的参数返回错误 json 对象。我该如何处理这两种情况?
我正在尝试使用本教程,但主要问题是将正常和错误 json 映射到对象。
我的正常响应示例:
[ {
"type" : "cat",
"color": "black"
},
{
"type" : "cat",
"color": "white"
} ]
Run Code Online (Sandbox Code Playgroud)
错误响应示例:
{"error" = "-1", error_description = "Low version"}
Run Code Online (Sandbox Code Playgroud)
我得到了什么:
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 …
Run Code Online (Sandbox Code Playgroud)