android OKHttp之日志拦截

    OkHttp的日志拦截通过HttpLoggingInterceptor对象进行拦截,该对象并不存在OkHttp包中,需要单独引用logging-interceptor包。

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

   创建 HttpLoggingInterceptor对象,进行日志打印

HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.e("msg", "++++++++" + message);
            }
        });
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttp进行日志拦截配置

 OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
     
        httpClient.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        httpClient.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
  
        httpClient.addNetworkInterceptor(new LogInterceptor());
        httpClient.addInterceptor(logging);//日志拦截

HttpLoggingInterceptor的日志级别,一共有四个分别为:NONE,BASIC,HEAD,BODY,这几者的区别如下

NONO:不打印任何日志

BASIC:请求行,响应行

    -->请求方式 请求地址 http/1.1 body长度

    <-- 服务器返回状态 地址 时间 长度

HEAD:

    请求行,请求头 ,响应行

BODY:

    请求行,请求头,请求体,响应行,响应体,

由上可以看出BODY级别是最全的


猜你喜欢

转载自blog.csdn.net/u013209460/article/details/80848842