Okhttp Logging Interceptor

来源:互联网 发布:机械杆做丝安全数据 编辑:程序博客网 时间:2024/05/23 00:43

Okhttp Logging Interceptor

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

示例

OkHttpClient client = new OkHttpClient();HttpLoggingInterceptor logging = new HttpLoggingInterceptor();logging.setLevel(Level.BASIC);client.interceptors().add(logging);/* 可以通过 setLevel 改变日志级别 共包含四个级别:NONE、BASIC、HEADER、BODYNONE 不记录BASIC 请求/响应行--> POST /greeting HTTP/1.1 (3-byte body)<-- HTTP/1.1 200 OK (22ms, 6-byte body)HEADER 请求/响应行 + 头--> Host: example.comContent-Type: plain/textContent-Length: 3<-- HTTP/1.1 200 OK (22ms)Content-Type: plain/textContent-Length: 6BODY 请求/响应行 + 头 + 体*/// 可以通过实现 Logger 接口更改日志保存位置HttpLoggingIntercetptor logging = new HttpLoggingInterceptor(new Logger() {    @Override    public void log(String message) {        Timber.tag("okhttp").d(message);    }});
0 1