gpt4 book ai didi

Android 改造添加 header 和 HttpLoggingInterceptor

转载 作者:行者123 更新时间:2023-11-29 23:30:21 28 4
gpt4 key购买 nike

我目前正在研究 android 中的改造:

这是我当前的代码:

 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 newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + Globals.BEARER_TOKEN)
.build();
return chain.proceed(newRequest);
}
}).build();

如何将我的 HttpLoggingInterceptor 添加到客户端,同时将我的 header 添加到客户端?

最佳答案

您可以添加两个拦截器,调用方法 addInterceptor 两次:

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 newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + Globals.BEARER_TOKEN)
.build();
return chain.proceed(newRequest);
}
})
.addInterceptor(interceptor).build();

关于Android 改造添加 header 和 HttpLoggingInterceptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52812248/

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