Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

java - Consuming One-Shot ResponseBody from Okhttp causes issues with Retrofit

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I accomplished this by creating a new response using the Response.Builder. I am able to use responseBodyString for my checks; then I return newResponse, which is given the body that I consumed.

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...