美文网首页
okhttp3 HttpLoggingInterceptor的用

okhttp3 HttpLoggingInterceptor的用

作者: Sunny600 | 来源:发表于2018-09-06 14:02 被阅读0次

HttpLoggingInterceptor 

        即 Http记录拦截器,用于记录应用中的网络请求的信息。


例子:

OkHttpClient client =newOkHttpClient();

HttpLoggingInterceptor logging =newHttpLoggingInterceptor();

logging.setLevel(Level.BASIC);

client.interceptors().add(logging);

/* 可以通过 setLevel 改变日志级别:共包含四个级别:NONE、BASIC、HEADER、BODY

NONE 不记录

BASIC 请求/响应行

--> POST /greeting HTTP/1.1 (3-byte body)

<-- HTTP/1.1 200 OK (22ms, 6-byte body)

HEADER 请求/响应行 + 头

--> Host: example.com

Content-Type: plain/text

Content-Length: 3

<-- HTTP/1.1 200 OK (22ms)

Content-Type: plain/text

Content-Length: 6

BODY 请求/响应行 + 头 + 体

*/

// 可以通过实现 Logger 接口更改日志保存位置

HttpLoggingIntercetptor logging =newHttpLoggingInterceptor(newLogger() {

@Override

public void log(String message){

        Log.v("okhttp",message);

    }

});

相关文章

网友评论

      本文标题:okhttp3 HttpLoggingInterceptor的用

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