retrofit2 addnetworkinterceptor和addinterceptor的区别
发布网友
发布时间:2022-05-18 22:23
我来回答
共1个回答
热心网友
时间:2023-11-12 17:53
private static OkHttpClient mOkHttpClient;
//短缓存有效期为1秒钟
public static final int CACHE_STALE_SHORT = 1;
//长缓存有效期为7天
public static final int CACHE_STALE_LONG = 60 * 60 * 24 * 7;
private MyRetrofitManager() {
initOkHttpClient();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_NIUPAI_URL)
.client(mOkHttpClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
// .addConverterFactory(new MyCustomFactory())
.addConverterFactory(GsonConverterFactory.create())
.build();
mJtmlService = retrofit.create(JtmlServer.class);
}
private void initOkHttpClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
if (mOkHttpClient == null) {
synchronized (MyRetrofitManager.class) {
if (mOkHttpClient == null) {
// 指定缓存路径,缓存大小100Mb
Cache cache = new Cache(new File(App.getContext().getCacheDir(), "HttpCache"),
1024 * 1024 * 100);
mOkHttpClient = new OkHttpClient.Builder()
.cache(cache)
.addInterceptor(mRewriteCacheControlInterceptor)
.addNetworkInterceptor(mRewriteCacheControlInterceptor)
.addInterceptor(interceptor)
.retryOnConnectionFailure(true)
.connectTimeout(15, TimeUnit.SECONDS)
.build();
}
}
}