Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept and retry call by means of OkHttp Interceptors

I need to retry request inside of OkHttp Interceptor. For example there is incoming request which needs Authorization token. If Authorization token is expired, server returns response with 403 code. In this case I am retrieving a new token and trying to make call again by using the same chain object.

But OkHttp throws an exception, which states that you cannot make two requests with the same chain object.

java.lang.IllegalStateException: network interceptor org.app.api.modules.ApplicationApiHeaders@559da2 must call proceed() exactly once 

I wonder if there is a clean solution to this problem of retrying network request inside of OkHttp Interceptor?

public final class ApplicationApiHeaders implements Interceptor {     private static final String AUTHORIZATION = "Authorization";     private TokenProvider mProvider;      public ApplicationApiHeaders(TokenProvider provider) {         mProvider = provider;     }      @Override     public Response intercept(Chain chain) throws IOException {         Token token = mProvider.getApplicationToken();         String bearerToken = "Bearer " + token.getAccessToken();          System.out.println("Token: " + bearerToken);         Request request = chain.request();         request = request.newBuilder()                 .addHeader(AUTHORIZATION, bearerToken)                 .build();          Response response = chain.proceed(request);         if (!response.isSuccessful() && isForbidden(response.code())) {             Token freshToken = mProvider.invalidateAppTokenAndGetNew();             String freshBearerToken = freshToken.getAccessToken();              Request newRequest = chain.request();             newRequest = newRequest.newBuilder()                     .addHeader(AUTHORIZATION, freshBearerToken)                     .build();              response = chain.proceed(newRequest);         }          return response;     }      private static boolean isForbidden(int code) {         return code == HttpURLConnection.HTTP_FORBIDDEN;     } } 
like image 861
Araz Abishov Avatar asked Feb 16 '15 07:02

Araz Abishov


People also ask

How does OkHttp interceptor work?

Interceptors, according to the documentation, are a powerful mechanism that can monitor, rewrite, and retry the API call. So, when we make an API call, we can either monitor it or perform some tasks. In a nutshell, Interceptors function similarly to airport security personnel during the security check process.

What is interceptor in networking?

The Interceptor network services feature allows you to redirect all or some of the IPv4 traffic (UDP, optimized TCP, pass-through TCP, and preexisting connections) to the SteelHeads in the cluster. This redirection allows you to enable SteelFlow or NetFlow export for the traffic on the SteelHead.

What are interceptors retrofit?

Retrofit is a popular, simple and flexible library to handle network requests in android development.

What are request interceptors?

A request interceptor is a piece of code that gets activated for every single HTTP request received by your application. Interceptors are very useful when you need to perform some common processing for every HTTP request.


1 Answers

Use .interceptors() instead of .networkInterceptors() which are allowed to call .proceed() more than once.

For more information see: https://square.github.io/okhttp/interceptors/

like image 160
Jake Wharton Avatar answered Sep 24 '22 03:09

Jake Wharton