rlalfo / google-http-java-client

Automatically exported from code.google.com/p/google-http-java-client
0 stars 0 forks source link

Enable response caching on Android ICS #152

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
External references, such as a standards document, or specification?

http://android-developers.blogspot.com/2011/09/androids-http-clients.html

http://javadoc.google-http-java-client.googlecode.com/hg/1.11.0-beta/com/google/
api/client/extensions/android/http/AndroidHttp.html#newCompatibleTransport()

http://code.google.com/p/google-http-java-client/wiki/HTTP#Pluggable_HTTP_Transp
ort

Java environments (e.g. Java 6, Android 2.3, App Engine, or All)?

Android

Please describe the feature requested.

Details can be found in the blog above.  It explains how to enable HTTP 
response caching on ICS.  Code copied below:

private void enableHttpResponseCache() {
    try {
        long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
        File httpCacheDir = new File(getCacheDir(), "http");
        Class.forName("android.net.http.HttpResponseCache")
            .getMethod("install", File.class, long.class)
            .invoke(null, httpCacheDir, httpCacheSize);
    } catch (Exception httpResponseCacheNotAvailable) {
    }
}

Additionally, we should a link to this blog post in the JavaDoc of 
AndroidHttp.newCompatibleTransport().

Finally, we should update our docs that explain which HTTP transport to use on 
Android.  We're not updating the wiki pages, but rather the developer 
documentation we are working on that is not on the wiki.

Original issue reported on code.google.com by yan...@google.com on 23 Sep 2012 at 6:45

GoogleCodeExporter commented 9 years ago
There is more detailed documentation on HttpResponseCache here:

http://developer.android.com/reference/android/net/http/HttpResponseCache.html

A small note: enableHttpResponseCache() should be a static method added to 
AndroidHttp, and we should probably also add Context context and long 
httpCacheSize parameters.  Also, since we are compiling against the latest 
Android SDK version 4.1, we don't need to use reflection, though we should 
still catch Exception so the method can be called on earlier versions of 
Android without causing harm (though of course caching won't be abled if it is 
not available).

Original comment by yan...@google.com on 23 Sep 2012 at 6:56

GoogleCodeExporter commented 9 years ago
We can also add a utility method to flush the cache to be called from onStop():

    public static void flushHttpResponseCache() {
       HttpResponseCache cache = HttpResponseCache.getInstalled();
       if (cache != null) {
           cache.flush();
       }
    }

(and again catch NoClassDefFoundException for compatibility with older Android 
versions.

Original comment by yan...@google.com on 23 Sep 2012 at 7:06

GoogleCodeExporter commented 9 years ago
Check it out!  There is a port of HttpResponseCache to be used on any Java 
environment:

https://github.com/candrews/HttpResponseCache

It uses Apache License, Version 2.0, so it may be a good candidate for 
inclusion.

Original comment by yan...@google.com on 23 Sep 2012 at 7:10

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 6 Oct 2012 at 1:37