Skip to content Skip to sidebar Skip to footer

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,

Solution 1:

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;

Solution 2:


Post a Comment for "Consuming One-Shot ResponseBody From Okhttp Causes Issues With Retrofit"