Handling error response in Retrofit android -


i using retrofit networking library. running smoothly. have new requirement, server send me response in json, in case of failure. want grab whole response, right retrofiterror in failure callback, doesn't give server response in of retrofiterror attributes.

any kind of appreciated. thanks.

i guess trying fields json response returned api during failure. think process failure pretty similar process success. first, response object needs have field map json error field, or field want extract response (success or failure). in success case converted response directly, in case of failure, need call getresponse() on returned retofiterror object converted response, , extract returned data.

here example.

i have rails backend responds token when successful or list of errors if not successful. here rails controller method if helps, bottom line there json entry errors want examine if create fails.

  def create     @user = user.new(user_params)     if @user.save       sign_in_api(@user)       render json: { token: @issued_remember_token }, status: 201     else       render json: { errors: @user.errors.full_messages }  , status: 406     end   end 

here retrofit call

    @post(api_url + "/signup")     void signup(@body userrequestparams requestparams,             callback<signupresponse> tokenparms); 

and here signupresponse object

public class signupresponse extends baseresponse {     private string token;      public string gettoken() {         return token;     }  }  public class baseresponse {     private list<string> errors = new arraylist<string>();     private boolean msuccessful = false;     public response getrawresponse() {         return rawresponse;     }      public void setrawresponse(response rawresponse) {         this.rawresponse = rawresponse;     }      private response rawresponse;       public baseresponse() {         super();     }      public list<string> geterrors() {         return errors;     }      public void setsuccessful(boolean successful) {         msuccessful = successful;     }      public boolean issuccessful() {         return msuccessful;     } } 

the result retrofit fills in errors field part of callback process, same if success. can check see if response successful or not , call appropriate methods on response object. errors null if successful valid if not.

for completeness here other 2 methods/classes involved in example.

    @subscribe     public void onsignup(signuprequest event) {         system.out.println("inside api repo - making signup request");         mrailsapi.signup(event, new railsapicallback<signupresponse>(mbus, new signupresponse()));     }   public class railsapicallback<t extends baseresponse> implements callback<t> {      private bus mbus;     private t mresponse;      public railsapicallback(bus bus, t response) {         super();         mbus = bus;         mresponse = response;          }      @override      public void failure(retrofiterror retrofiterror) {         system.out.println(retrofiterror.tostring());         t response = retrofiterror != null && retrofiterror.getbody() != null ? (t) retrofiterror.getbody() : mresponse  ;         response.setrawresponse(retrofiterror.getresponse());         response.setsuccessful(false);         system.out.println("posting response bus");         mbus.post(response);     }      @override     public void success(t convertedresponse, response rawresponse) {         system.out.println(rawresponse.getbody());         t response = convertedresponse != null ? convertedresponse : mresponse ;         response.setsuccessful(true);         response.setrawresponse(rawresponse);         mbus.post(response);      } } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -