okhttpclient打印网络请求的完整log日志

1. OkhttpClient添加拦截器 设置logging信息打印日志

 private HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> Timber.e("okhttp response json = %s", message));

    private OkHttpClient getClient() {
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(1000, TimeUnit.SECONDS)
                .readTimeout(1000, TimeUnit.SECONDS)
                .writeTimeout(1000, TimeUnit.SECONDS)
                .connectionPool(new ConnectionPool(10000, 10000, TimeUnit.SECONDS))
                .retryOnConnectionFailure(true)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request().newBuilder()
                                .addHeader("Accept", "application/json")
                                .addHeader("Connection", "keep-alive")
                                .addHeader("Authorization", UserManager.getHeader())
                                .build();
                        return chain.proceed(request);
                    }
                })
                .addInterceptor(logging)
                .build();
        return client;
    }

}

2.设置log打印的详细程度

1)BODY级别会打印所有东西的,

logging.setLevel(HttpLoggingInterceptor.Level.BODY);

2)  HEADERS级别,就只打印请求头、响应头

logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);

3. 设置Timer打印(在application中)

Timber.plant(new Timber.DebugTree());

 

你可能感兴趣的:(work遇到问题)