I couldn't find an updated answer for this. I'm sending requests with Volley to a web API. It returns JSON. I'm using the cache feature like below, but I would like to make sure that the listview is refreshed every so often (say 30 mins for now). How can I invalidate the cache for this given URL to have my app handle that automatically (without a refresh button). This question was helpful in pointing out the difference between invalidate and remove.
MainActivity.java
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(URL_FEED);
if (entry != null) {
// fetch the data from cache
try {
String data = new String(entry.data, "UTF-8");
try {
parseJsonFeed(new JSONArray(data));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else
{
// making fresh volley request and getting json
JsonArrayRequest getRequest = new JsonArrayRequest(URL_FEED,
new Response.Listener<JSONArray>()
{
@Override public void onResponse(JSONArray response) {
VolleyLog.d(TAG, "Response: " + response.toString());
if (response != null) {
parseJsonFeed(response);
}
Log.d("Response", response.toString());
}
},......ErrorListener
To refresh listview,you can use volley's serverDate to get the date for when the response was originally received as
this return datetime in long. And in your code use minutedifference function as
and Call this function in your code as
It will invalidate the cache,if previous url response >=30 minutes.
This (invalidate) allows to keep using this data until a new call is made and the cached response is overridden with the new response.