retrofit: OkHttpCall problems

Hello, guys.

I tried use Retrofit 2.0 + OkHttp 2.5 in my project. Before that I used Retrofit 1.9 with OkHttp 2.5 - I had not problems.

About my Retrofit configuration:

  1. I added GsonConverter and GsonConverterFactory from your retrofit-connectors sample.
  2. I created my Gson object with Type adapters.
  3. I created my custom OkHttpClient with Application interceptor(for adding headers)
  4. I created my custom BaseUrl with my service endpoint.
  5. I created ServiceAPI interface with method like this : @GET(“/api/v1/users/{fbid}/init/”) Call<AppData> getAppData(@Path(“fbid”) String fbid, @Query(“delta_since_timestamp”) long delta, @Query(“chat_enabled”) long chat);
  6. I created Retorfit object like this : private static final Retrofit RETROFIT_MAIN = new Retrofit.Builder().baseUrl (sMainEndpoint).client(generateNewOkClient(sMainEndpoint.getUrl())) .converterFactory(GsonConverterFactory.create(gsonBuilder)).callbackExecutor (Executors.newFixedThreadPool(2)) .build(); I added custom Executor only for tests.
  7. I created my service like this: private static final ServiceAPI SERVICE_MAIN = RETROFIT_MAIN.create(ServiceAPI.class);

Before next note it’s ok.

  1. Now I want use getAppData() method: Call<AppData> call = getService().getAppData(playerFbid, delta, BuildConfig.CHAT_VERSION); call.enqueue(new Callback<AppData>() { @Override public void onResponse(Response<AppData> response) { AppData appData = response.body(); if (appData != null) {

            }
        }
    
        @Override
        public void onFailure(Throwable error) {
    
        }
    });
    

After that I alwayshave problem in this code (OkHttpCall class, parseResponse method):

// Remove the body's source (the only stateful object) so we can pass the response along.
rawResponse = rawResponse.newBuilder()
    .body(new NoContentResponseBody(rawBody.contentType(), rawBody.contentLength()))
    .build();

After this code I always have Exception like this - IllegalStateException -“Cannot read raw response body of a converted body.”

This exception was generated in NoContentResponseBody class, source() method. It very strange because I have Response with valid http url and code = 200(Result=OK).

Why I have this exception? May be I have wrong Retrofit configuartion or problem contains in Retrofit 2.0 library?

Thank you for attension.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 21 (7 by maintainers)

Most upvoted comments

I’m having the same issue without NewRelic when I try to read the raw response body, for some reason I need to have both the converted response and save the raw JSON string as well. So I implemented a ResponseCallback<T> that extends Callback<T> and in onResponse(Response<T> response) I can receive the converted body as type T, but when I try to read the raw response response.raw().body().string(), I get the following exception:

java.lang.IllegalStateException: Cannot read raw response body of a converted body.
at retrofit.NoContentResponseBody.source(NoContentResponseBody.java:41)
at com.squareup.okhttp.ResponseBody.bytes(ResponseBody.java:54)
at com.squareup.okhttp.ResponseBody.string(ResponseBody.java:83)
at .....ResponseCallback.getRawBodyAsString(ResponseCallback.java:181)
at .....ResponseCallback.onResponse(ResponseCallback.java:81)
at retrofit.ExecutorCallAdapterFactory$ExecutorCallback$1.run(ExecutorCallAdapterFactory.java:84)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

By the way I was using NewRelic, but it’s completely disabled now until they fix the conflict.

but when I try to read the raw response response.raw().body().string(), I get the following exception:

I faced the same issue today (I don’t use NewRelic too). Calling body() from response instead of raw() works fine. response.body().string()

if your response is not successful (response.isSuccess() is false), you have to use response.errorBody() instead