0
我使用retrofit2,並希望增加一些頭使用OkHttp3 addInterceptor 但不工作Android的改造
這是我的代碼
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.removeHeader("Authorization")
.removeHeader("Content-type")
.removeHeader("User-Agent")
.addHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
.addHeader("Accept-Language", "en-US")
.addHeader("User-Agent", ApiConfig.userAgent)
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
if (apiInterface == null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiConfig.baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
apiInterface = retrofit.create(ApiInterface.class);
}
return apiInterface;
請幫我添加自定義標題。 親切的問候
請點擊此鏈接: https://stackoverflow.com/questions/32963394/how-to-use-interceptor-to-add-headers-in -retrofit-2-0 – MrTy
還沒有工作 – MiladCoder
把你的logcat超過 – MrTy