Retrofit 2.x:用于请求和响应的日志头 [英] Retrofit 2.x : Log Header for request and response

查看:107
本文介绍了Retrofit 2.x:用于请求和响应的日志头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用翻新版2.x,我想记录请求和响应的标题和正文.

I am using retrofit 2.x and i want to log the header and body of request and response .

  HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
            .addNetworkInterceptor(new Interceptor() {
                @Override
                public okhttp3.Response intercept(Chain chain) throws IOException {
                    Request request = chain.request().newBuilder()
                            .addHeader("key", "value")
                            .addHeader("HEADER","HEADER Value")
                            .build();
                    return chain.proceed(request);
                }


            }).build();

这就是我的工作方式,我的问题是请求的标头没有在Android Monitor中记录,但是其余的都记录了.

And this how i am doing,my problem is header of request are not being logged in Android Monitor but rest everything is logged .

Gradle版本

 compile ('com.squareup.retrofit2:retrofit:2.0.0-beta3') {
    // exclude Retrofit’s OkHttp peer-dependency module and define your own module import
    exclude module: 'okhttp'
}
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'
compile ('com.squareup.okhttp3:logging-interceptor:3.0.1'){
    exclude module: 'okhttp'
}

由于错误问题,使用RC1和3.0.1报告了错误链接

Using RC1 and 3.0.1 due to bug issue reported Bug Link

推荐答案

可以帮助某人...

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

添加两者以查看完整的日志,并在最后添加此拦截器(不知道为什么,但类似这样).

Add both to see complete logs and add this interceptor at the last (don't know why but its like this).

这篇关于Retrofit 2.x:用于请求和响应的日志头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆