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.<br/>
<http://stackoverflow.com/questions/5575181/does-android-keeps-the-
images-downloaded-from-http-in-cache><br/>
<http://stackoverflow.com/questions/5389524/how-to-do-image-caching-in-
android><br/>

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?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to