我在OkHttpClient中添加了两个Inteceptors和一个NetworkInceptor,但只有cachingInterceptor有效。我不知道如何添加另一个Interceptor来修改我的请求标题。
并且有一张图片显示我的请求标题未在代码下面修改。
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();
}
}
答案 0 :(得分:1)
使用addInterceptor
,而不是addNetworkInterceptor
。
你几乎总是想要一个应用程序拦截器,而不是一个网络拦截器。
网络拦截器仅在请求到达网络时调用,而不是在缓存命中时调用。