Retrofit打印网络相关Log

dependencies {
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
}

HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            //打印retrofit日志
            Log.i("RetrofitLog","retrofitNetwork --->"+message);
        }
    });

        /* 可以通过 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 请求/响应行 + 头 + 体
*/
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            //增加返回值为String的支持
            .addConverterFactory(ScalarsConverterFactory.create())
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build();




猜你喜欢

转载自blog.csdn.net/du_zilin/article/details/79022933
今日推荐