How can I forcefully cache all my HTTP responses?
<p>I'm using the <code>DefaultHTTPClient</code> to make some HTTP GET requests. I'd like to forcefully cache all the HTTP responses for a week. After going through the docs and some SO answers, I've done this:</p>

<p>I installed an <code>HttpResponseCache</code> via the <code>onCreate</code> method of my main activity.</p>

<pre><code>    try {
        File httpCacheDir = new File(getApplicationContext().getCacheDir(), "http");
        long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
        HttpResponseCache.install(httpCacheDir, httpCacheSize);
    } catch (IOException e) {
        Log.i("dd", "HTTP response cache installation failed:" + e);
    }
</code></pre>

<p>I added a custom <code>HttpResponseInterceptor</code> for my HTTP client, but I still don't get a cache hit. Here's my response interceptor that decompresses GZIPped content, strips caching headers and adds a custom one:</p>

<pre><code>class Decompressor implements HttpResponseInterceptor {

    public void process(HttpResponse hreResponse, HttpContext hctContext) throws HttpException, IOException {

        hreResponse.removeHeaders("Expires");
        hreResponse.removeHeaders("Pragma");
        hreResponse.removeHeaders("Cache-Control");
        hreResponse.addHeader("Cache-Control", "max-age=604800");

        HttpEntity entity = hreResponse.getEntity();

        if (entity != null) {
            Header ceheader = entity.getContentEncoding();
            if (ceheader != null) {
                HeaderElement[] codecs = ceheader.getElements();
                for (int i = 0; i &lt; codecs.length; i++) {
                    if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                        hreResponse.setEntity(new HttpEntityWrapper(entity) {
                            @Override
                            public InputStream getContent() throws IOException, IllegalStateException {
                                return new GZIPInputStream(wrappedEntity.getContent());
                            }

                            @Override
                            public long getContentLength() {
                                return -1;
                            }
                        });
                        return;
                    }
                }
            }
        }
    }

}
</code></pre>

<p>Here's how I make my request:</p>

<pre><code>String strResponse = null;

HttpGet htpGet = new HttpGet(strUrl);
htpGet.addHeader("Accept-Encoding", "gzip");
htpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
DefaultHttpClient dhcClient = new DefaultHttpClient();
dhcClient.addResponseInterceptor(new Decompressor(), 0);
HttpResponse resResponse = dhcClient.execute(htpGet);
Log.d("helpers.network", String.format("Cache hit count: %d", HttpResponseCache.getInstalled().getHitCount()));

strResponse = EntityUtils.toString(resResponse.getEntity());

return strResponse;
</code></pre>

<p>I can't seem to pinpoint what I'm doing wrong. Would any of you know?</p>
