懒人必备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;
       }

完工看下效果


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,067评论 19 139
  • 之前在掘金、简书上看到很多篇介绍retrofit的文章,但是一直没有静下心来研究这到底是什么东西。最近时间比较充裕...
    皇马船长阅读 1,825评论 0 3
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,977评论 6 342
  • 6.1 公钥密钥加密原理 6.1.1 基础知识 密钥:一般就是一个字符串或数字,在加密或者解密时传递给加密/解密算...
    AndroidMaster阅读 4,044评论 1 8
  • 今天看相册时,竟然发现嘴有点小歪,好难过啊,又多了一项任务,矫正自己的嘴角,前些天还在矫正自己的外八,哎呀,真的是...
    小菜籽5257阅读 202评论 0 0