Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit - Intercept responses globally

I'd like to intercept all responses received by the retrofit engine, and scan for HTTP error code, for example error 403.

I'm aware I can use the failure(RetrofitError error) callback of every request and check for 403's but I'd like to wrap the response globally.

I can see that request interception is possible, but I do not see a similar option for response.

Any suggestions?

like image 510
Aviran Avatar asked Aug 30 '15 08:08

Aviran


People also ask

Why interceptors are used in retrofit?

We can use interceptors to do a variety of things, such as centrally monitoring API calls. In general, we need to add a logger for each network call, but by using the interceptor, we can add a logger once and it will work for all network calls.

What are interceptors in retrofit?

What are Interceptors? According to documentation, Interceptors are a powerful mechanism that can monitor, rewrite, and retry the API call. So basically, when we do some API call, we can monitor the call or perform some tasks.

What is chain in interceptor?

The Chain object in retrofit is an implementation of the Chain of Responsibility design pattern, and each interceptor is a processing object which acquires the result of the previous interceptor through chain.

How do I add an interceptor to Okhttpclient?

header("User-Agent", "OkHttp Headers. java") . addHeader("Accept", "application/json; q=0.5") . addHeader("Accept", "application/vnd.


1 Answers

I was able to accomplish that by adding an interceptor to the OkHttpClient that retrofit is using.

Kotlin + Retrofit 2.x

val clientBuilder = OkHttpClient.Builder() clientBuilder.addInterceptor { chain ->             val request = chain.request()             val response = chain.proceed(request)             if (response.code() == 403) {                 handleForbiddenResponse()             }             response         } 

Retrofit 2.x:

   OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();    clientBuilder.             addInterceptor(new Interceptor() {                 @Override                 public Response intercept(Chain chain) throws IOException {                    Request request = chain.request();                    Response response = chain.proceed(request);                    if (response.code() == 403) {                          handleForbiddenResponse();                    }                    return response;                 }             });       Retrofit retrofit = new Retrofit.Builder()             .baseUrl(API_BASE_URL)             .addConverterFactory(GsonConverterFactory.create())             .client(clientBuilder.build();)             .build(); 

Retrofit 1.x:

public class ForbiddenInterceptor implements Interceptor {      @Override     public Response intercept(Chain chain) throws IOException {         Request request = chain.request();         Response response = chain.proceed(request);         if (response.code() == 403) {              handleForbiddenResponse();         }         return response;     } }    OkHttpClient okHttpClient = Utils.createUnsafeOkHttpClient(); okHttpClient.interceptors().add(new ForbiddenInterceptor());  RestAdapter.Builder restAdapterBuilder = new RestAdapter.Builder()         .setEndpoint(API_BASE_URL)         .setClient(new OkClient(okHttpClient)); 
like image 153
Aviran Avatar answered Oct 07 '22 17:10

Aviran