gpt4 book ai didi

android - 改造 2.x : Log Header for request and response

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:50:45 25 4
gpt4 key购买 nike

我正在使用改造 2.x,我想记录请求和响应的 header 和正文。

  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();

这就是我的做法,我的问题是请求 header 未记录在 Android 监视器中,但其余所有内容均已记录。

渐变版本

 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 Bug Link

最佳答案

哦,如果有人感兴趣的话,我发现了错误:

 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.addInterceptor(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();

必须在请求拦截器后面加上日志拦截器(你的拦截器变量),所以正确答案是:

 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(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);
}


})
.addInterceptor(interceptor)
.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.build();

关于android - 改造 2.x : Log Header for request and response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35265906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com