我正在使用带有Okhttp拦截器的Retrofit来检测我的oauth令牌是否已过期。如果令牌已过期,我想请求新令牌,再次尝试请求,然后将该响应发送到Retrofit。
这是我的拦截器类:
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;
}
}
问题是调用response.body.string()将消耗ResponseBody,因为它是Okhttp docs状态的一次性值。
这意味着在代码结束时返回的响应将在传递给改进时不再对主体进行容器化。有没有什么方法可以消耗身体,同时仍然回复它?
谢谢!
答案 0 :(得分:11)
我是通过使用Response.Builder创建新响应来实现此目的的。我可以使用responseBodyString
进行检查;然后我返回newResponse
,这是我消耗的身体。
Request request = chain.request();
Response response = chain.proceed(request);
ResponseBody responseBody = response.body();
String responseBodyString = response.body().string();
Response newResponse = response.newBuilder().body(ResponseBody.create(responseBody.contentType(), responseBodyString.getBytes())).build();
...
return newResponse;
答案 1 :(得分:2)
你可以使用Logging Interceptor https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor