Skip to content

Instantly share code, notes, and snippets.

@polbins
Created March 10, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save polbins/437297ed7587c100e57d to your computer and use it in GitHub Desktop.
Save polbins/437297ed7587c100e57d to your computer and use it in GitHub Desktop.
Sample use of OkHttp + Retrofit Application Interceptor for Resending Requests w/ Refreshed Tokens
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(mRefreshAndRetryInterceptor);
mRestAdapter = new RestAdapter.Builder()
...
.setClient(new OkClient(okHttpClient))
.build();
private final Interceptor mRefreshAndRetryInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
if (response.code() == 401) {
Request newRequest = reWriteRequestWithNewToken(request);
response = chain.proceed(newRequest);
}
return response;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment