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:
- I added GsonConverter and GsonConverterFactory from your retrofit-connectors sample.
- I created my Gson object with Type adapters.
- I created my custom OkHttpClient with Application interceptor(for adding headers)
- I created my custom BaseUrl with my service endpoint.
- 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);
- 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.
- I created my service like this: private static final ServiceAPI SERVICE_MAIN = RETROFIT_MAIN.create(ServiceAPI.class);
Before next note it’s ok.
-
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)
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:
By the way I was using NewRelic, but it’s completely disabled now until they fix the conflict.
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 useresponse.errorBody()
instead