Java 无法在android中使用cachingHttpClient缓存HttpResponse
这对我不起作用 每次它都从服务器获取数据,而不是从缓存获取数据Java 无法在android中使用cachingHttpClient缓存HttpResponse,java,android,Java,Android,这对我不起作用 每次它都从服务器获取数据,而不是从缓存获取数据 我使用的是jar-httpclient-cache-4.1-beta1。您还没有向我们展示您的HTTP服务器的运行情况。mydomain.com/content上的服务是否在HTTP响应上设置了正确的缓存控制头?为了使缓存工作,您需要让HTTP服务器或web应用程序指示是否可以缓存数据,以及可以使用适当的头缓存数据的长度 此外,请检查CachingHttpClient上记录的API,以查看它希望从web服务器获得哪些头 在用php编
我使用的是jar-httpclient-cache-4.1-beta1。您还没有向我们展示您的HTTP服务器的运行情况。mydomain.com/content上的服务是否在HTTP响应上设置了正确的缓存控制头?为了使缓存工作,您需要让HTTP服务器或web应用程序指示是否可以缓存数据,以及可以使用适当的头缓存数据的长度
此外,请检查CachingHttpClient上记录的API,以查看它希望从web服务器获得哪些头 在用php编写的web应用程序中需要设置哪些参数,在服务器端的请求和响应中需要设置哪些参数。以及如何处理我的android中的缓存。我已经添加了从我的android代码中删除Header的代码。即使是它返回的cache_miss。您可以共享CachingHttpClient的链接吗?它似乎不在Android SDK中。没关系,是从。我也有同样的问题。
public class CacheDemo {
public static void main(String[] args) {
CacheConfig cacheConfig = new CacheConfig();
cacheConfig.setMaxCacheEntries(1000);
cacheConfig.setMaxObjectSizeBytes(1024 * 1024);
HttpClient cachingClient = new CachingHttpClient(new DefaultHttpClient(), cacheConfig);
HttpContext localContext = new BasicHttpContext();
sendRequest(cachingClient, localContext);
CacheResponseStatus responseStatus = (CacheResponseStatus) localContext.getAttribute(
CachingHttpClient.CACHE_RESPONSE_STATUS);
checkResponse(responseStatus);
sendRequest(cachingClient, localContext);
responseStatus = (CacheResponseStatus) localContext.getAttribute(
CachingHttpClient.CACHE_RESPONSE_STATUS);
checkResponse(responseStatus);
}
static void sendRequest(HttpClient cachingClient, HttpContext localContext) {
HttpGet httpget = new HttpGet("http://www.mydomain.com/content/");
HttpResponse response = null;
try {
response = cachingClient.execute(httpget, localContext);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpEntity entity = response.getEntity();
try {
EntityUtils.consume(entity);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void checkResponse(CacheResponseStatus responseStatus) {
switch (responseStatus) {
case CACHE_HIT:
System.out.println("A response was generated from the cache with no requests "
+ "sent upstream");
break;
case CACHE_MODULE_RESPONSE:
System.out.println("The response was generated directly by the caching module");
break;
case CACHE_MISS:
System.out.println("The response came from an upstream server");
break;
case VALIDATED:
System.out.println("The response was generated from the cache after validating "
+ "the entry with the origin server");
break;
}
}
}