私のアプリでは、HttpResponseCache を実装して応答をキャッシュし、サーバーにアクセスする代わりに使用できるようにしました。特定の api について、サーバーはヘッダー Cache-Control を no-cache;must-revalidate として返します。ヘッダー ETag もあります。問題は、この API の応答がキャッシュされていないことです。その結果、API を要求するたびに、サーバーは 200 を返します。no-cache,must-revalidate は、応答がキャッシュされない/キャッシュされないことを意味しますか?
http リクエストのリクエスト ヘッダーとレスポンス ヘッダーを以下に示します。
リクエストヘッダー:
GET HTTP/1.1 User-Agent Accept application/json Cache-Control max-age=0 Cookie
Host
Connection Keep-Alive Accept-Encoding gzip
応答ヘッダー:
HTTP/1.1 200 OK サーバー Apache-Coyote/1.1 Cache-Control no-cache, must-revalidate ETag "c683a0301c68c566fcc706f5cd82f1f8" Content-Type application/json;charset=UTF-8 Transfer-Encoding chunked Content-Encoding gzip Vary Accept-Encoding Date 2014 年 2 月 24 日 (月) 04:44:03 GMT
HTTP_GET リクエストを送信中:
URL url = new URL(this.url);
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(timeout);
conn.setConnectTimeout(timeout);
conn.setInstanceFollowRedirects(true);
conn.setRequestProperty("User-Agent", this.userAgent);
conn.setUseCaches(true);
conn.setDefaultUseCaches(true);
conn.connect();
this.responseCode = conn.getResponseCode();
this.extractResponseHeaders(conn.getHeaderFields());
InputStream inputStream = null;
if (this.responseCode >= 400 ) {
inputStream = conn.getErrorStream();
} else {
inputStream = conn.getInputStream();
}
try {
if(null != inputStream){
this.response = convertToString(inputStream);
}
} catch (JSONException e) {
e.printStackTrace();
}