Updated 15 December 2016
To avoid downloading duplicate data, you can use caching. Always cache static resources, including on-demand downloads such as full size images, for as long as reasonably possible.
But sometimes caching cause your app, displaying stale data thats why managing cache is important in android as well. Be sure to extract the time at which the requested content was last updated, and when it expires and then refresh the content accordingly.
HttpURLConnection:
You can do this on Http response headers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
long currentTime = System.currentTimeMillis(); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); long expires = conn.getHeaderFieldDate("Expires", currentTime); long lastModified = conn.getHeaderFieldDate("Last-Modified", currentTime); setDataExpirationDate(expires); if (lastModified < lastUpdateTime) { // Skip update } else { // Parse update } |
Volley :
Volley is so transparent about its cache that you can do almost anything you want. The cache is implemented implicitly for each JsonRequest and StringRequest.
Clear volley cache as:
1 |
request_queue.getCache().clear(); |
You can disable cache explicitly by:
1 |
request.setShouldCache(false); |
Or you can disable cache for a particular URL as:
1 |
request_queue.getCache().remove(<YOUR URL>); |
And for reading cache data:
1 2 3 4 5 |
Entry entry = queue.getCache().get(<URL>); if(entry!=null){ String data = new String(entry.data, "UTF-8"); // process data } |
And for showing cache data until new request is made you can use:
1 |
request_queue.getCache().invalidate(url, true); |
So its pretty clear that we can do anything with the cache in volley.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.