美文网首页程序员
懒人必备Retorfit快速打印网络请求相关日志信息

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

作者: 后来Memory | 来源:发表于2018-07-04 16:19 被阅读0次

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

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

完工看下效果


image.png

相关文章

网友评论

    本文标题:懒人必备Retorfit快速打印网络请求相关日志信息

    本文链接:https://www.haomeiwen.com/subject/ruoeuftx.html