Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get raw response and request using Retrofit 2.0

Tags:

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);

                }
            });
like image 510
praveen kumar Avatar asked May 07 '16 13:05

praveen kumar


People also ask

How do you send a request body in Get method retrofit?

This means your @GET or @DELETE should not have @Body parameter. You can use query type url or path type url or Query Map to fulfill your need. Else you can use other method annotation.

How do I return a retrofit response?

Actually you cant return a value , instead you can try making a method for passing/storing data in DTO & call that method in your respose. IsSuccessful . Pass the object of response json to the method for storing your required stuffs like token, username n etc,.


1 Answers

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.

like image 117
aldok Avatar answered Sep 28 '22 15:09

aldok