如何在 Android 上检查 DefaultHttpClient 中的缓存

发布于 2024-11-19 16:25:28 字数 1615 浏览 3 评论 0原文

我的 Android 应用程序需要使用 DefaultHttpClient 缓存来自 Web 服务调用的响应文本。缓存应在 Http 响应标头中设置的到期时间之前有效。

我发现了类似的问题,但他们抱怨 DefaultHttpClient 正在缓存他们的响应。有趣的是我需要它但无法工作。或者建议基于文件的解决方案。
Android 是否将从 HTTP 下载的图像保留在缓存中?< /a>
如何在android中进行图像缓存

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

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

我的 GAE servlet 代码是,

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!");
}

每次单击按钮都会给我提供 200 的状态代码。我希望这应该只是第一次出现。

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

我编辑了servlet并发布了;客户端读取最新的更改。 我在 Chrome 浏览器上测试了 servlet 应用程序,缓存工作正常。

我在请求头中添加了Cache-control属性,但没有得到预期的结果。

如何确保DefaultHttpClient缓存响应内容并且在到期时间之前不会再次向服务器发送请求?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你搞怪i 2024-11-26 16:25:28

这个 CachingHttpClient 可能就是您正在寻找的,它是只是 DefaultHttpClient 的装饰器。

请注意,android仅包含HttpClient 4.0,为了使示例代码在android中运行,您需要将依赖项HttpClient 4.1和HttpClient Cache 4.1添加到您的项目中。

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.

软糖 2024-11-26 16:25:28

您可以使用这个

它是一个 704kb 的库,包含为 Android 编译的 httpclient 4.1 的并行实现。
它包含 CachingHttpClient 和许多错误修复。但是,仅当内存缓存对您有用时才使用。因此,如果您的应用程序在一个会话期间多次调用相同的 api ,那么性能影响将是显而易见的。

You can use this.

Its a 704kb library and contains a parallel implementation of httpclient 4.1 compiled for Android.
It contains CachingHttpClient and many bug fixes as well. However only use if in-memory cache is useful for you. So if your application makes the same api call many times during one session the performance impact will be visible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文