Utilizing Http response cache
A typical mobile application will probably consume some resources from a web server using http requests. This repeated task has some drawbacks:
- Bandwidth has a cost, polling the same data over and over again results in a waste of user’s quota.
- the responsiveness and user experience is compromised once the user keeps waiting for data. If the requested data hasn’t changed on the server, then there’s no reason for us to fetch it.
Of course, local caching of data is a must, but we also need to be able to refresh the data if it has changed on the server.
A simple solution which comes out of the box for API 4+ is http response caching.
Http response caching is a mechanism which enables us as developers to save a footprint of the response utilizing the abilities of the Http protocol.
Basically, is holds a simple cache of the response which can be defined and enabled in code, and it prevents from fetching unchanged data from the server which was already fetched and processed into the cache.
In Android 4+ we should use the HttpUrlConnection class in order to get data from a given Url address.
first , we need to enable this feature by using the following code, preferably in a pre loading of the application, for example: in a Class which derives from the Application class , in the onCreate() method before any requests are done to the server
private void enableHttpCaching()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
try {
File httpCacheDir = new File(getApplicationContext().getCacheDir()
, "http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);
} catch (IOException e) {
Log.i(Constants.TAG_REPONSE_CACHING_FAILED
, "OVER ICS: HTTP response cache failed:" + e);
}
}
else
{
File httpCacheDir = new File(getApplicationContext().getCacheDir()
, "http");
try {
com.integralblue.httpresponsecache.HttpResponseCache.install
(httpCacheDir, 10 * 1024 * 1024);
} catch (IOException e) {
Log.i(Constants.TAG_REPONSE_CACHING_FAILED
, "UNDER ICS : HTTP response cache failed:" + e);
}
}
}
notice that we perform a check for the installed Android version on the device in order to support devices with API version prior to ICS (Android 4.0), because we are using the HttpResponseCache Class.
The inner part of the method also involves creating A file to host the actual cached data.
Here as a simple example of using httpUrlConnection in order to be able to utilize the httpResopnseCache.
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setUseCaches(true);
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Comments
This manganonism also supports https responses, you can have a look at the documentation here:
http://developer.android.com/reference/android/net/http/HttpResponseCache.html