요청을 사용하여 Retrofit 2에서 오류 응답을 처리하는 방법은 무엇입니까?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();
}
참조하지만, 가장 큰 문제는 과정이 JSON 객체입니다 (일반적인 경우) 애완 동물 배열 및 오류 응답 (요청 나쁜 paraters있는 경우). –