I am using an Retrofit with an Okhttp interceptor in order to detect if my oauth token has expired. If the token has expired, I want to request a new token, try the request again, then send that response to Retrofit.
Here is my interceptor class:
public class CustomInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// try the request
Response response = chain.proceed(request);
if (response.body().string().contains(Constants.TOKEN_AUTH_ERROR_MESSAGE)) {
Log.v("retrofit_error", "token expired");
//get current token, create headers
OAuthTokenResponse expiredToken = SharedPreferencesUtil.getOAuthToken();
OAuthTokenResponse newOauthToken = RestClient.getInstance().getTokenService().refreshOauthToken(expiredToken.getRefreshToken());
//store new token, return
SharedPreferencesUtil.saveOAuthToken(newOauthToken);
// create a new request and modify it accordingly using the new token
Request.Builder newRequestBuilder = request.newBuilder()
.removeHeader("Authorization");
Request newRequest = newRequestBuilder.addHeader("Authorization", SharedPreferencesUtil.getOAuthToken().getAccessToken()).build();
// retry the request
return chain.proceed(newRequest);
}
// otherwise just pass the original response on
return response;
}
}
The issue is that calling response.body.string() will consume the ResponseBody due to it being a one-shot value as the Okhttp docs state.
This means that the Response returned at the end of the code will no longer container the body when it is passed off to retrofit. Is there any way that I can consume the body while still returning it with the response?
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…