懒人必备Retorfit快速打印网络请求相关日志信息

话不多少直接上代码。相关配置就不在赘述了。加上这个配置

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

第一步 初始化HttpLoggingInterceptor

@Provides
        @Singleton
        public HttpLoggingInterceptor providerHttpInter(){
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                @Override
                public void log(String message) {
                    Log.d("retorfit","retorfit"+message);
                }
            });
            //四个等级
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            return httpLoggingInterceptor;
        }

此处日志打印有四个等级:NONE、BASIC、HEADERS、BODY。 就不在一一打印出来了。BODY就是最详细了。

第二步 为Http设置

 @Provides
       @Singleton
       public OkHttpClient provideOk(HttpLoggingInterceptor httpLoggingInterceptor){
           OkHttpClient okHttpClient = new OkHttpClient.Builder()
                   .addInterceptor(httpLoggingInterceptor)
                   .connectTimeout(30 * 1000, TimeUnit.MILLISECONDS)
                   .readTimeout(20 * 1000, TimeUnit.MILLISECONDS)
                   .build();
            return okHttpClient;
       }

完工看下效果


懒人必备Retorfit快速打印网络请求相关日志信息_第1张图片
image.png

你可能感兴趣的:(懒人必备Retorfit快速打印网络请求相关日志信息)