OkHttp設定支援Etag快取

嘻哈夫子發表於2018-11-20

 

重點已經畫圈

 /**
     * 獲取okHttpClient
     *
     * @return the ok http client
     */
    private final static int CACHE_SIZE_BYTES = 1024 * 1024 * 2;
    public static OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS)
                    .readTimeout(15, TimeUnit.SECONDS)
                    .retryOnConnectionFailure(true)
                    .writeTimeout(15, TimeUnit.SECONDS)
                    .addInterceptor(new TokenInterceptor())
                    .cache(new Cache(context.getCacheDir(),CACHE_SIZE_BYTES))
                    .addNetworkInterceptor(new HttpLoggingInterceptor())
                    .build();
        }
        return okHttpClient;
    }

 

相關文章