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.

Requestrequest= chain.request();
Responseresponse= chain.proceed(request);

ResponseBodyresponseBody= response.body();
StringresponseBodyString= response.body().string();
ResponsenewResponse= 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"