我一直在嘗試使用OkHttp和Retrofit來緩存http請求。但我似乎不明白爲什麼它不工作。無法在android中實現HTTP緩存
@Headers("Cache-Control: public, max-age=640000, s-maxage=640000 , max-stale=10000000")
@FormUrlEncoded
@POST("/news/getNewslist/")
void newsListByGenre(@Field("news_genre") String genre,
Callback<ArrayList<NewsStory>> callback);
這是其中一個請求,它具有所有必需的標題。此外,爲了測試將某些內容寫入文件緩存,我手動將緩存分配給了OkHttpClient。
OkHttpClient name = new OkHttpClient();
try {
if (!cache.exists())
cache.createNewFile();
name.setResponseCache(new HttpResponseCache(cache,
10 * 1024 * 1024));
} catch (IOException e) {
e.printStackTrace();
}
我創建的文件緩存只有36個字節,所以我肯定沒有任何緩存。
我也試圖確保服務器有所需的標題,儘管我希望它在沒有服務器干擾的情況下工作,但我也在請求中設置了高速緩存控制標題。這是來自改造的調試日誌。
null: HTTP/1.1 200 OK
Cache-Control: public, max-age=360000
Connection: Keep-Alive
Content-Length: 5167
Content-Type: application/json
Date: Fri, 28 Jun 2013 01:00:22 GMT
Keep-Alive: timeout=5, max=99
Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
X-Android-Received-Millis: 1372381311315
X-Android-Response-Source: NETWORK 200
X-Android-Selected-Transport: http/1.1
X-Android-Sent-Millis: 1372381311048
X-Powered-By: PHP/5.4.7
我已經讀了http的緩存機制,但似乎我失去了一些東西。
嘿你有任何機會,你可以幫助我與類似的情況下... http://stackoverflow.com/questions/22445177/trying-to-make-use-of-httpcache-android – Lion789