Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

android - How can we handle different response type with Retrofit 2?

I have a webservice that returns either a list of serialized MyPOJO objects:

[
  { //JSON MyPOJO },
  { //JSON MyPOJO }
]

either an error object :

{  
  'error': 'foo', 
  'message':'bar' 
}

Using retrofit2, how can I retrieve the error ?

Call<List<MyPOJO>> request = ...
request.enqueue(new Callback<List<MyPOJO>>() {
  @Override
  public void onResponse(Response<List<MyPOJO>> response) {
      if (response.isSuccess()) {
          List<MyPOJO> myList = response.body();
          // do something with the list...
      } else {
          // server responded with an error, here is how we are supposed to retrieve it
          ErrorResponse error = ErrorResponse.fromResponseBody(apiService.getRetrofitInstance(), response.errorBody());
          processError(error);
          // but we never get there because GSON deserialization throws an error !
      }
  }

  @Override
  public void onFailure(Throwable t) {
    if(t instanceof IOException){
        // network error 
    }else if(t instanceof IllegalStateException){
        // on server sending an error object we get there
        // how can I retrieve the error object ?
    }else { 
        // default error handling 
    }       
  }
}

Here is the GSON exception:

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Retrofit instance is created using GsonConverterFactory

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had a similar issue and I solved it by using a generic Object and then testing what type of response I had using instanceof

Call<Object> call = api.login(username, password);
call.enqueue(new Callback<Object>() 
{
    @Override
    public void onResponse(Response<Object> response, Retrofit retrofit)
    {
         if (response.body() instanceof MyPOJO )
         {
            MyPOJO myObj = (MyPOJO) response.body();
            //handle MyPOJO 
         }
         else  //must be error object
         {
            MyError myError = (MyError) response.body();
            //handle error object
         }
    }

    @Override
    public void onFailure(Throwable t) 
    {
     ///Handle failure
    }
});

In my case I had either MyPOJO or MyError returned and I could be sure it would be one of these.

In other cases then I had the backend return the same Response Object no matter if the request was successful or not.
Then inside this response object I had my actual data within an "Object" field. Then I can use instance of to determine what type of data I had. In this case I always had the same object being returned, no matter what the call was.

public class MyResponse {

    private int responseCode;
    private String command;
    private int errorno;
    private String errorMessage;
    private Object responseObject;   //This object varies depending on what command was called
    private Date responseTime;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...