當前位置:編程學習大全網 - 編程軟體 - retrofit2 addnetworkinterceptor和addinterceptor的區別

retrofit2 addnetworkinterceptor和addinterceptor的區別

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();

}

}

}

  • 上一篇:豐田銳誌清洗節氣門後怠速高什麽原因?
  • 下一篇:如何導入海龜編輯器
  • copyright 2024編程學習大全網