1
在我的應用程序的onCreate,我創建一個10MB緩存:緩存與OkHttp(不含改裝)
try
{
File httpCacheDir = new File(getApplicationContext().getCacheDir(), Constants.AppName);
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);
}
catch (IOException ex)
{
Log.i(Constants.AppName, "HTTP response cache installation failed: " + ex);
}
而我給資源調用:
OkHttpClient client = new OkHttpClient();
client.setResponseCache(HttpResponseCache.getInstalled());
HttpURLConnection connection = client.open(url);
connection.addRequestProperty("Cache-Control", "max-age=60");
InputStream inputStream = connection.getInputStream();
我會初始化這個調用兩次,在10秒鐘之內,OkHttp-Response-Source標題總是NETWORK 200.
for (Map.Entry<String, List<String>> k : connection.getHeaderFields().entrySet())
{
for (String v : k.getValue())
{
Log.d(Constants.AppName, k.getKey() + ": " + v);
}
}
我在想什麼在這裏?
欣賞響應。我看着我的源頭,果然,它沒有被退回。 :-( –
爲了讓它工作起來,我掙扎了很多。基本上,所有的工作都必須在服務器配置中完成,以便返回正確的頭文件集。一旦我意識到這一切,就更容易了:) –
對我而言不幸的是,我不控制服務器。謝謝你的幫助。 –