0

I am using the following libraries for retrofit.

'com.squareup.retrofit2:retrofit:2.5.0'
'com.squareup.okhttp:okhttp:2.7.5'
'com.squareup.retrofit2:converter-gson:2.5.0'
'com.squareup.okhttp3:logging-interceptor:3.10.0'

How can I get the raw response in onResponse callback? I already searched for it and got a lots of solutions which doesn't help now. I tried response.raw().body.string() and response.body().source().toString() which throws can not read body from a converted body. I also tried response.body().string() but in this case .string() is unresolved. I can log the response using interceptor but I need that response in my onResponse() callback, not just printing in logcat. My Retrofit Client:

public static ApiService getClient(Context context) {

    if (retrofit == null) {
        if (BuildConfig.FLAVOR.equalsIgnoreCase("dev")){
            retrofit = new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BASE_URL)
                    .client(okHttpClient)
                    .build();
        }else {
            retrofit = new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BASE_URL)
                    .client(SelfSigningClientBuilder.createClient(context))
                    .build();
        }

    }
    return retrofit.create(ApiService.class);
}

My Retyrofit Interface:

@retrofit2.http.POST("weekly_driver_earning_report/")
@retrofit2.http.FormUrlEncoded
Call<List<DailyEarnings>> getDriverWeeklyEarnings(@retrofit2.http.Field("access_token") String access_token, @retrofit2.http.Field("start_date") String start_date, @retrofit2.http.Field("end_date") String end_date);
Mihodi Lushan
  • 670
  • 5
  • 24
  • 1
    Post your retrofit interface. Also, use consistent library versions. Retrofit 2.5.0 depends on okhttp > 3.0 – denvercoder9 Sep 22 '19 at 05:02
  • 2
    Possible duplicate of [Get raw HTTP response with Retrofit](https://stackoverflow.com/questions/33282889/get-raw-http-response-with-retrofit) – Martin Zeitler Sep 22 '19 at 05:16
  • 1
    I think one has to remove the ORM auto-mapping to get the `Call` in there. – Martin Zeitler Sep 22 '19 at 05:17
  • I have added interface code snippets @sonnet – Mihodi Lushan Sep 22 '19 at 05:22
  • @MartinZeitler I have mentioned earlier that response.body().string() is not available now – Mihodi Lushan Sep 22 '19 at 05:23
  • 1
    You have to change `Call>` into `Call` to get the whole http response in the callback. – denvercoder9 Sep 22 '19 at 05:24
  • @sonnet I have changed my okhttp from okhttp:2.7.5 to okhttp3:4.2.0 and replaced my model class to ResponseBody but still getting the same error `onResponse: java.lang.IllegalStateException: Cannot read raw response body of a converted body.` – Mihodi Lushan Sep 22 '19 at 05:49
  • 1
    Plese put the error also. If your json response is not validated GsonConverterFactory will not able to deserialize instead use scalar converter factory in retrofit to print raw response. – Kaushik Burkule Sep 22 '19 at 05:49
  • @KaushikBurkule my onResponse works fine in other statements, I am getting proper views, so I have a Validated Json, also checked it in Postman – Mihodi Lushan Sep 22 '19 at 05:51

1 Answers1

0

Following this, I solved my problem later. I defined my Call type to specific model class and that's why response.body().string() was inaccessible, Changing my Call type from List to ResponseBody, I could access response.body().string().

Mihodi Lushan
  • 670
  • 5
  • 24