Created
March 10, 2015 14:27
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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