Retrofit 與 OKHttp 可以在離線時使用快取資料嗎?
本題探討了使用 Retrofit 和 OKHttp 來快取 HTTP 回應用於離線存取。問題中提供的程式碼遵循常見方法,但無法離線檢索快取的回應表示需要一些額外的配置。
Edit for Retrofit 2.x
對於 Retrofit 2.x,離線快取的首選方法是使用 OkHttp 攔截器。
-
建立一個攔截器:
<code class="java">private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); if (Utils.isNetworkAvailable(context)) { int maxAge = 60; // read from cache for 1 minute return originalResponse.newBuilder() .header("Cache-Control", "public, max-age=" + maxAge) .build(); } else { int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale return originalResponse.newBuilder() .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale) .build(); } } };</code>
-
設定客戶端:
<code class="java">OkHttpClient client = new OkHttpClient(); client.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR); //setup cache File httpCacheDirectory = new File(context.getCacheDir(), "responses"); int cacheSize = 10 * 1024 * 1024; // 10 MiB Cache cache = new Cache(httpCacheDirectory, cacheSize); //add cache to the client client.setCache(cache);</code>
- 設定客戶端:
<code class="java">Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build();</code>
<code class="java">File httpCacheDirectory = new File(context.getCacheDir(), "responses"); Cache cache = null; try { cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024); } catch (IOException e) { Log.e("OKHttp", "Could not create http cache", e); } OkHttpClient okHttpClient = new OkHttpClient(); if (cache != null) { okHttpClient.setCache(cache); }</code>
OKHttp 2.0.x(原始答案)
在OKHttp 2.0.x 中,setResponseCache方法現在是setCache:原始答案
<code class="java">RestAdapter.Builder builder= new RestAdapter.Builder() .setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addHeader("Accept", "application/json;versions=1"); if (MyApplicationUtils.isNetworkAvailable(context)) { int maxAge = 60; // read from cache for 1 minute request.addHeader("Cache-Control", "public, max-age=" + maxAge); } else { int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale request.addHeader("Cache-Control", "public, only-if-cached, max-stale=" + maxStale); } } });</code>原始答案強調需要在伺服器回應中加入Cache-Control: public 標頭,以便OkClient 離線存取快取資料。 此外,可以使用 RequestInterceptor 來實現參數化標頭配置:
以上是OKHttp Retrofit 可以離線使用快取資料嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

關於曲線積分中變量替換的疑問本文將針對一個曲線積分計算中變量替換的步驟進行詳細解釋。提問者在計算一...

微信小程序申請流程及費用詳解朋友委託開發微信小程序商城,在申請過程中遇到一些關於費用和流程的問題,...

Java緩存數據無法獲取的原因分析及優化方案在Java項目中,將大量數據緩存到內存中以便快速訪問是一個常見的�...

深入Java:值得探索的虛擬機世界很多Java開發者在掌握了基礎語法和常用框架後,都希望進一步提升自己的技術�...

EasyExcel模板填充合併單元格時的常見問題在使用EasyExcel進行Excel...

系統對接中的字段映射挑戰及其解決方案在系統對接過程中,經常會遇到需要將一個系統的接口字段映射到另一...

SpringBoot應用中PgJDBC連接池拋出PSQLException:ERROR:cancelingstatementduetouserrequest異常在使用SpringBoot MyBatis-Plus ...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。