21

I am trying to get the raw response using Retrofit2.0.2.

So far I tried to print the response using following line of code but it prints the address and not the exact response body.

Log.i("RAW MESSAGE",response.body().toString());

compile 'com.squareup.retrofit2:retrofit:2.0.2'

    Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();


            GitApi gitApi = retrofit.create(GitApi.class);

            Call<Addresses> call = gitApi.getFeed(user);

    call.enqueue(new Callback<Addresses>() {

                @Override
                public void onResponse(Response<Addresses> response, Retrofit retrofit) {
                    try {
                        mDisplayDetails.setText(response.body().getSuburbs().get(0).getText());

                    **Log.i("RAW MESSAGE",response.body().toString());**

                    } catch (Exception e) {
                        mDisplayDetails.setText(e.getMessage());
                    }
                    mProgressBar.setVisibility(View.INVISIBLE);

                }

                @Override
                public void onFailure(Throwable t) {
                    mDisplayDetails.setText(t.getMessage());
                    mProgressBar.setVisibility(View.INVISIBLE);

                }
            });
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
praveen kumar
  • 211
  • 1
  • 2
  • 3
  • Possible duplicate of [retrofit 2.0b2 : How to get InputStream from the response?](http://stackoverflow.com/questions/33030137/retrofit-2-0b2-how-to-get-inputstream-from-the-response) – Kevin Robatel May 09 '16 at 14:48
  • for anyone coming here, complete answer could be found here: http://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request/36821182#36821182 – TommySM Dec 05 '16 at 09:12

4 Answers4

15

That's because it's already converted to an object by converter. To get the raw json, you need an interceptor on your Http Client. Thankfully you don't need to write your own class, Square already provide HttpLoggingInterceptor class for you.

Add this on your app-level gradle

compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'

And use it in your OkHttpClient

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor).build();

Don't forget to change your HttpClient in Retrofit.

Retrofit retrofit = new Retrofit.Builder()
            .client(client)               
            .baseUrl("https://yourapi.com/api/")
            .build();

In the Log Cat you'll see the raw json response. Further information is available on Square's OkHttp github.

Caution!

Don't forget to remove Interceptors (or change Logging Level to NONE) in production! Otherwise people will be able to see your request and response on Log Cat.

aldok
  • 17,295
  • 5
  • 53
  • 64
  • how to get latest version of interceptor? i couldn't find it in square's github – zihadrizkyef Aug 02 '17 at 01:53
  • 1
    @zihadrizkyef 3.8.1, you can pretty much find all dependencies in mvnrepository, for example https://mvnrepository.com/artifact/com.squareup.okhttp3/logging-interceptor – aldok Aug 02 '17 at 01:55
2

You have to use "ResponseBody" from okhttp3 in your call. Then, get "response.body().string()" to get the JSONObject as your server gives to you. It's a good way to catch errors if there are any errors parsing server response to your model object.

oskarko
  • 3,382
  • 1
  • 26
  • 26
1

Simply use:

Log.i("RAW MESSAGE", response.raw().body().string());

Or:

Log.i("RAW MESSAGE", response.body().string());
Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57
  • 7
    response.raw().body().string()); give me Cannot read raw response body of a converted body, because You can only read the Response once since it is a stream. You are reading it in your UtilityMethods.convertResponseToString method, so you need to create a new Response with the same content. – Kamil Nękanowicz Nov 25 '16 at 13:17
  • 4
    >> you need to create a new Response with the same content. How can I do it? – valerybodak Dec 18 '17 at 18:52
1

Guess you want to see the raw response body for debugging purpose. There are two ways to do this.

  1. Using okhttp3.logging.HttpLoggingInterceptor as @aldok mentioned.

  2. If you want to check some properties of the response object, or convert the json(response body) to POJO manually, you may want to do it like this:

Don't use the addConverterFactory while initializing the retrofit client, comment this line.

retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(BASE_URL)
                //.addConverterFactory(GsonConverterFactory.create())
                .build();

Then consuming the response like this:

Call<ResponseBody> topRatedResponseCall = apiService.getTopRatedMoves(API_KEY);
    topRatedResponseCall.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                Log.d(LOG_TAG, response.body().string());
                int code = response.code();
                testText.setText(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {

        }
    });

Hope this would help~

yu zhao
  • 11
  • 2