1

i am not getting logs of requests and response requests in android studio. I am even using HttpLogginInterceptor in Retrofit2. But i am not able to see logs of request body and response body in Logcat.

Heres my Retrofit Client:

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class RestClient {


    private static RestClient restClient;
    private Retrofit retrofitAuth;
    private Retrofit retrofit;

    /**
     * only to be used for auth
     */


    public static WebServices webServices() {
        return getRestClient().getRetrofitInstance().create(WebServices.class);
    }

    public static WebServices webAuthServices() {
        return getRestClient().getAuthRetrofitInstance().create(WebServices.class);
    }

    private static RestClient getRestClient() {
        if (restClient == null) {
            restClient = new RestClient();
        }
        return restClient;
    }

    private Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BuildConfig.API_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(getOkHttpClient())
                    .build();
        }
        return retrofit;
    }

    private Retrofit getAuthRetrofitInstance() {
        if (retrofitAuth == null) {
            retrofitAuth = new Retrofit.Builder()
                    .baseUrl(BuildConfig.API_URL)
                    .client(getAuthOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofitAuth;
    }

    private OkHttpClient getOkHttpClient() {
        return new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new ConnectivityInterceptor())
                .addInterceptor(new HeaderAdder())
                .addInterceptor(new AuthInterceptor())
                .addInterceptor(getHttpLoggingInterceptor())
                .build();
    }

    private OkHttpClient getAuthOkHttpClient() {
        return new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new HeaderAdder())
                .addInterceptor(new ConnectivityInterceptor())
                .addInterceptor(getHttpLoggingInterceptor())
                .build();
    }

    private HttpLoggingInterceptor getHttpLoggingInterceptor() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        return logging;
    }

    public static void destroyRestClient() {
        restClient = null;
    }


}

As you can see the second last Function which returns HttpLoggingInterceptor, and i give it to my okhttp client and then that client is added to retrofit instance. But still i am not able to see logs. Please help.

Maximus
  • 189
  • 1
  • 3
  • 15
  • Your code should work unless other interceptors break something – Selvin Mar 18 '19 at 11:52
  • @Selvin that is the problem mate, i am not able to understand why it isnt logging, i am able to make perfect api calls, getting response also. But i want to see logs, i am debugging everytime to see response. Am i supposed to enable or disable something? or i should see in debug mode of logcat? i tried debug, error , verbose already, really smh. – Maximus Mar 18 '19 at 11:54
  • Have u check this https://stackoverflow.com/questions/32514410/logging-with-retrofit-2 – UdayaLakmal Mar 18 '19 at 11:54
  • Yes, he did ... his code already have HttpLoggingInterceptor – Selvin Mar 18 '19 at 11:55
  • @Selvin can dynamic header adding can be a problem? – Maximus Mar 18 '19 at 11:58

1 Answers1

2

Please Add this Lib in Gradle file

 // Retrofit-2
    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'





  val interceptor = HttpLoggingInterceptor()
    interceptor.level = HttpLoggingInterceptor.Level.BODY

    val client = OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .addInterceptor(interceptor)
            .build()
   GlobalApp.api = Retrofit.Builder()
            .baseUrl(END_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build().create(Api::class.java)
Deepak Ror
  • 2,084
  • 2
  • 20
  • 26
  • There is no `RestAdapter` in retrofit2 – Selvin Mar 18 '19 at 11:21
  • implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1' you need only this lib to start logcat – Deepak Ror Mar 18 '19 at 11:26
  • What is the use of gson and rxjava? one is used to parse/map data into object and later to do operations post data comes. So those 2 dependencies makes no sense. And also i already have implementation 'com.squareup.okhttp3:okhttp:3.10.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0' in my app – Maximus Mar 18 '19 at 11:34
  • 2
    But wheres the solution ? the answer u have written , isnt it already i have in my question ? Indeed i have more. – Maximus Mar 18 '19 at 11:39
  • change HttpLoggingInterceptor.Level.BODY to Level.FULL – Deepak Ror Mar 18 '19 at 11:40
  • There is nothing called as FULL, i think BODY is the highest form of Logging available, i think you need to be updated before answering. – Maximus Mar 18 '19 at 11:42
  • HttpLoggingInterceptor.Level.BODY to RestAdapter.LogLevel.FULL – Deepak Ror Mar 18 '19 at 11:44
  • There is nothing called as RestAdapter in Retrofit2. – Maximus Mar 18 '19 at 11:45
  • @Selvin can u plz suggest something? – Maximus Mar 18 '19 at 11:49
  • @Maximus why are you using retrofit logger to see req and response you can use android studio tool Network Profiling here is link https://developer.android.com/studio/profile/network-profiler –  Mar 18 '19 at 11:59
  • @AndroidTeam This might be a good alternative, thank you, but i need logs only, i have a specific requirement where i am supposed to send my logs to my server whenever there is a problem, so can you please check my code again and let me know whats the problem, i would really be thankful. – Maximus Mar 18 '19 at 12:05
  • In Android Profiling you can see all request and response which HTTP Logger provides, you can also check response time as well, so I think that is better option, we will try your code and will check what problem you are facing when we get time.Thanks –  Mar 18 '19 at 12:15