Intercept the HTTP response in Retrofit before passing it to the caller

Now we use the modification as follows:

service.executeSomeRequest(UserPreferenceRequest userPreferenceRequest, new Callback<UserPreferenceResponse>() { @Override public void success(UserPreferenceResponse responseCallback, Response response) { if (responseCallback.getStatus() == ResponseStatus.OK) { // Everything is OK, process response } else { ApiErrorProcessor.process(responseCallback.getError()); } } @Override public void failure(RetrofitError retrofitError) { ServerErrorProcessor.process(retrofitError); } }); 

But we have many requests, and almost every request that we implement requires us to write the same error code processing (for API and server errors) that duplicates the code.

We want to redefine only the methods of interest to us, and if the implementation has not been implemented, then the default implementation is executed.

Something like that:

 service.executeSomeRequest(UserPreferenceRequest userPreferenceRequest, new CustomCallback<UserPreferenceResponse>() { @Override public void success(UserPreferenceResponse responseCallback, Response response) { super.success(responseCallback, response); // Everything is OK, process response } }); 

CustomCallback will take care of the API and server errors, and if everything is in order, then only pass the result of the calling activity.

When building the RestAdapter there is setRequestInterceptor(); , which allows me to catch the request before its release, I was thinking about something similar, for example, setResponseInterceptor() , which will allow me to catch the answer before passing it in activity and treating common errors there, but did not find something like that.

+8
android retrofit
source share
2 answers

Your own callback can first process the response in the base class and then pass it to the abstract method.

 public interface StatusResponse { Status getStatus(); } public abstract class CustomCallback<T extends StatusResponse> implements Callback<T> { @Override public final void success(T data, Response response) { if (data.getStatus() == Status.OK) { success(data); } else { // Handle error.. } } public abstract void success(T data); } 
+10
source

You can combine upgrade requests with an event bus and have a clean and centralized point to handle your responses.

All you have to do is define the composite object as follows:

 public class GetUsers { // Retrofit Request public static final class Request {} // Retrofit Callback public static final class Callback implements retrofit.Callback<Response response> { @Override public void success(Response response, Response retrofitResponse) { // .... handle the code BusManager.post(new Event()); } @Override public void failure(RetrofitError error) {} BusManager.post(new RetrofitErrorEvent(error)); } // Otto Event public static final class Event {} 

This object defines the request, callback, and event, and this object is passed to the modification request.

  public void getUsers(){ request.users(new GetUsers.Request(), new GetUsers.Callback()); } 

After that, in your main class it will look like this:

 public void onResume(){ controller.getUsers(); } @Subscribe public void onGetPostsEvent(GetPosts.Event event){ textView.setText(event.getText()); } 

Check out this blog post for a more detailed explanation: Otto + Retrofit is an elegant web request solution , and you can find a working example here

+2
source

All Articles