Subby
Subby

Reputation: 5480

Retrofit - Different Response

I am consuming an API in Android using Retrofit. The success response looks different to the error/failure response. How can I therefore achieve this?

I currently have something to this affect:

Call<AuthenticateUserResponse> authenticateUser(String id);

Upvotes: 3

Views: 7817

Answers (1)

savepopulation
savepopulation

Reputation: 11921

You can extend your Responses with a base Response and check if there's an error or it's succeed. Here's an example base response bean below:

public class BaseResponse {
    @SerializedName("ResponseCode")
    private int code;
    @SerializedName("ResponseMessage")
    private String message;
    @SerializedName("ResponseText")
    private String text;
}

My api returns ResponseCode, ResponseMessage and ResponseText in every responser and i extend my responses from BaseResponse bean and check if there's an error.

You can modify your responses as your api's return schemes.

Edit: Here's your Response for your Api:

public class Error {
        @SerializedName("ResponseCode")
        private int code;
        @SerializedName("ResponseMessage")
        private String message;
        @SerializedName("ResponseText")
        private String text;
    }

public class YourWrapperResponse {
        @SerializedName("Error")
        private Error error;
        @SerializedName("AuthenticateUserResponse")
        private AuthenticateUserResponse authenticateUserResponse;
    }

And your call will be like:

Call<YourWrapperResponse> authenticateUser(String id);

The example above i gave you is an example of handle business errors which you get in your every successful response. Successful means Http Status 200. Also you do not need to return this Error object in your every response. If there's an error you can return in your response.

In Retrofit 2.0+ you need to check if your request is succeed. Here's an example below about it:

    Call<User> auth = YourApiProvider.getInstance().getServices().auth(userName, passowrd, grantType);
            auth.enqueue(new Callback<User>() {
                @Override
                public void onResponse(Call<User> call, Response<User> response) {
                    if (response.isSuccessful()) {
                        // Here you get a 200 from your server.
                        }
                    } else {
                        // Here you get an authentication error.
                        // You can get error with response.code();
                        // You can get your error with response.errorBody(); 
                        // or you can get raw response with response.raw()
                    }
                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {
                    // Here you get error such as TimeOut etc.
                }
            });

I hope this'll help you. Good Luck!

Edit: You can also use generics to handle base api responses. Here's my another answer about handling generic api responses.

Upvotes: 5

Related Questions