我在OkHttpClient中添加了两个Inteceptor和一个NetworkInceptor,但是只有cachingInterceptor有效。我不知道如何添加另一个拦截器来修改我的请求标头。
有一张图片显示我的请求标头未在代码下面进行修改。
import android.app.Application;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.orhanobut.logger.Logger;
import java.io.IOException;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import me.strugglingvincent.meizhichinese.NetworkApi;
import me.strugglingvincent.meizhichinese.Util.ConnectionUtil;
import okhttp3.Cache;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Scheduler;
import rx.schedulers.Schedulers;
/**
* NetModule Object
* Created by StrugglingVincent on 12/08/2016.
*/
@Module
public class NetModule {
private final String mBaseUrl;
public NetModule() {
mBaseUrl = "https://wtf.herokuapp.com/wtf/";
}
@Provides
@Singleton
NetworkApi provideNetworkApi(Retrofit retrofit) {
return retrofit.create(NetworkApi.class);
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache, Interceptor cachingInterceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(cachingInterceptor); //It works
builder.addInterceptor(new Interceptor() { //It doesn't work
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.addHeader("Accept-Encoding", "gzip, deflate")
.addHeader("Connection", "keep-alive")
.addHeader("Accept", "*/*")
.addHeader("Cookie", "add cookies here")
.addHeader("Referer","http://www.baidu.com")
.build();
System.out.println("interceptor:"+request.headers());
return chain.proceed(request);
}
});
builder.addNetworkInterceptor(new Interceptor() { //It doesn't work
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.addHeader("Accept-Encoding", "gzip, deflate")
.addHeader("Connection", "keep-alive")
.addHeader("Accept", "*/*")
.addHeader("Cookie", "add cookies here")
.addHeader("Referer","http://www.baidu.com")
.build();
System.out.println("network:"+request.headers());
return chain.proceed(request);
}
});
builder.cache(cache);
return builder.build();
}
@Provides
@Singleton
Cache provideCache(Application application) {
int cacheSize = 10 * 1024 * 1024;//10 mb
return new Cache(application.getCacheDir(), cacheSize);
}
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient, GsonConverterFactory gsonConverterFactory) {
System.out.println("shenmjibawanyier?"+okHttpClient!=null);
System.out.println("fdsja;lfsjkl;"+okHttpClient.interceptors());
System.out.println("wfewajfewafewea"+okHttpClient.networkInterceptors());
Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
retrofitBuilder.baseUrl(mBaseUrl)
.client(okHttpClient)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create());
return retrofitBuilder.build();
}
@Provides
@Singleton
GsonConverterFactory provideGsonFactory() {
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
return GsonConverterFactory.create(gson);
}
/**
* Log Network Response
*
* @return Logging Interceptor
*/
@Provides
@Singleton
HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Logger.t(5).i(message);
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return httpLoggingInterceptor;
}
/**
* Cache Are available when Network Connection is not available
*
* @return Caching Interceptor
*/
@Provides
@Singleton
Interceptor getCachingInterceptor(final Application mApp) {
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (ConnectionUtil.isNetworkAvailable(mApp)) {
request = request.newBuilder().header("Cache-Control", "public, max-age=" + 60).build();
} else {
request = request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 7).build();
}
return chain.proceed(request);
}
};
}
@Provides
@Singleton
Scheduler provideScheduler() {
return Schedulers.io();
}
}
Here's my request headers
最佳答案
使用addInterceptor
,而不是addNetworkInterceptor
。
您几乎总是需要应用程序拦截器,而不是网络拦截器。
仅在请求到达网络时才调用网络拦截器,而缓存命中时则不调用。
关于android - 为什么okhttp3.Interceptor不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41714226/