Java 在OkHttp 2.0上获取过时的正文
My webservice发送以下标题:Java 在OkHttp 2.0上获取过时的正文,java,caching,okhttp,Java,Caching,Okhttp,My webservice发送以下标题: Cache-Control: public, max-age=60, max-stale=86400 现在,在我第一次打电话时,以下是回复: 代码=200 cacheResponse=null networkResponse=Response{code=200,…} body=correctdata 一分钟内的第二个电话是: 代码=200 cacheResponse=Response{code=200,…} networkResponse=null b
Cache-Control: public, max-age=60, max-stale=86400
现在,在我第一次打电话时,以下是回复:
String cb=cacheBody.String()上崩溃代码>。代码示例如下所示:
if(response.networkResponse() != null) {
System.out.println("There is no cache, or cache is invalidated.");
if(!response.isSuccessful()) {
System.out.println("Call is not successful");
if(response.cacheResponse() != null) {
System.out.println("There is a cache");
ResponseBody cacheBody = response.cacheResponse().body();
String cb = cacheBody.string();
System.out.println("cacheBody: " + cb);
return cb;
}
}
}
ResponseBody body = response.body();
if(body != null) {
String b = body.string();
System.out.println("body: " + b);
return b;
}
这种行为是有意的吗?如果是,如何获取缓存的响应数据?添加此请求标头:
Cache-Control: max-stale=86400
这将导致OkHttp只使用缓存。这里的关键是它应该是请求头,而不是响应头。