如何检查缓存中DefaultHttpClient在Android [英] How to check caching in DefaultHttpClient on Android

查看:185
本文介绍了如何检查缓存中DefaultHttpClient在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序需要缓存使用DefaultHttpClient Web服务调用的响应文本。缓存应该是有效的,直到在HTTP响应头设置过期时间。

我发现了类似的问题,但他们抱怨说DefaultHttpClient是缓存它们的响应。滑稽我需要它,但无法得到工作。还是有解决方案提出,是基于文件的。
<一href="http://stackoverflow.com/questions/5575181/does-android-keeps-the-images-downloaded-from-http-in-cache">Does Android的不断从HTTP缓存下载的图片吗?
怎么办安卓

我写了一个示例应用程序请求的URL上点击一个按钮,并打印响应状态和标题。

  DefaultHttpClient客户端=新DefaultHttpClient();
HTTPGET请求=新HTTPGET(URL);
HTT presponse响应;
响应= client.execute(要求);
的System.out.println(响应状态 - + response.getStatusLine()的getStatus code());
 

和我的GAE的servlet code是,

 公共无效的doGet(HttpServletRequest的REQ,HttpServletResponse的RESP)抛出IOException异常{
    resp.setContentType(text / plain的);
    resp.setHeader(过期,星期三,2012年7月11号12:00:00 GMT);
    resp.setHeader(缓存控制,最大年龄= 2592000);
    resp.getWriter()的println(你好!)。
}
 

点击按钮,每次给我200。我希望这应该是第一次的情况下,状态code而已。

 响应状态 -  200
*****响应头*****
内容类型 - 文本/纯;字符集= ISO-8859-1
失效 - 周三,2012 12:00:00 GMT 07月11日
缓存控制 - 最大年龄= 2592000
日期 - 星期三,2011年7月13日6时54分57秒GMT
服务器 - 谷歌前端
传输编码 - 分块
 

我编辑的servlet和公布;客户端读取最新的变化。 我测试了Chrome浏览器和缓存的servlet应用程序工作正常。

我添加的请求头的缓存控制的财产,但并没有得到预期的结果。

我如何确保DefaultHttpClient缓存响应内容,不发送请求到服务器再次,直到到期时间?

解决方案

本的 CachingHttpClient 可能是你在寻找什么,它只是一个装饰的DefaultHttpClient。

需要注意的是Android的仅包含的HttpClient 4.0中,为了使样本code工作在Android上,你需要添加依赖的HttpClient 4.1和HttpClient的缓存4.1到项目中。

My Android application requires to cache the response text from a web service call using DefaultHttpClient. The cache should be valid till the expiry time set in the Http response header.

I found similar questions but they were complaints that the DefaultHttpClient is caching their responses. Funny I need it but could not get working. Or there solutions suggested that are file based.
Does Android keeps the images downloaded from HTTP in cache?
how to do image caching in android

I wrote a sample app that requests for a url on click of a button and prints the response status and headers.

DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);         
HttpResponse response;
response = client.execute(request);
System.out.println("Response status - " + response.getStatusLine().getStatusCode());

And my GAE servlet code is,

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    resp.setHeader("Expires", "Wed, 11 Jul 2012 12:00:00 GMT");
    resp.setHeader("Cache-Control", "max-age=2592000");
    resp.getWriter().println("Hi!");
}

Clicking on the button the every time gives me the status code as 200. I expect this should be the case for the first time only.

Response status - 200
***** Response Headers *****
Content-Type - text/plain; charset=iso-8859-1
Expires - Wed, 11 Jul 2012 12:00:00 GMT
Cache-Control - max-age=2592000
Date - Wed, 13 Jul 2011 06:54:57 GMT
Server - Google Frontend
Transfer-Encoding - chunked

I edited the servlet and published; the client reads the latest change. I tested the servlet application on Chrome browser and caching works fine.

I added the Cache-control property in the request header, but did not get the expected result.

How do I ensure the DefaultHttpClient caches the response content and does not send request to the server again until the expiry time?

解决方案

This CachingHttpClient is probably what you looking for, it is simply a decorator to the DefaultHttpClient.

Note that android only include HttpClient 4.0, in order to make the sample code works in android, you need add dependencies HttpClient 4.1 and HttpClient Cache 4.1 into your project.

这篇关于如何检查缓存中DefaultHttpClient在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆