How to log request and response body with Retrofit-Android?

前端 未结 9 2063
盖世英雄少女心
盖世英雄少女心 2020-11-28 21:35

I can\'t find relevant methods in the Retrofit API for logging complete request/response bodies. I was expecting some help in the Profiler (but it only offers meta-data abou

9条回答
  •  感情败类
    2020-11-28 22:13

    If you are using Retrofit2 and okhttp3 then you need to know that Interceptor works by queue. So add loggingInterceptor at the end, after your other Interceptors:

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            if (BuildConfig.DEBUG)
                loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    
     new OkHttpClient.Builder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .addInterceptor(new CatalogInterceptor(context))
                    .addInterceptor(new OAuthInterceptor(context))
                    .authenticator(new BearerTokenAuthenticator(context))
                    .addInterceptor(loggingInterceptor)//at the end
                    .build();
    

提交回复
热议问题