android - Volley Cache not get updated -
hi using volley jar in android app cache json data offline mode.it works perfectly.here code
cache cache1 = appcontroller.getinstance().getrequestqueue().getcache(); entry entry1 = cache1.get(url); if (entry1 != null) { // fetch data cache try { data2 = new string(entry1.data, "utf-8"); try { parsejsonfeed(new jsonobject(data2)); } catch (jsonexception e) { e.printstacktrace(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } } else { appcontroller.getinstance().getrequestqueue().getcache().remove(url); // making fresh volley request , getting json jsonobjectrequest jsonreq1 = new jsonobjectrequest(method.get, url, null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { volleylog.d(tag, "response: " + response.tostring()); if (response != null) { parsejsonfeed(response); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { volleylog.d(tag, "error:" + error.getmessage()); } }); // adding request volley request queue appcontroller.getinstance().addtorequestqueue(jsonreq1); } private void parsejsonfeed(jsonobject response) { try { feedarray = response.getjsonarray("events"); (int = 0; < feedarray.length(); i++) { feedobj = (jsonobject) feedarray.get(i); event_id.add(feedobj.getint("event_id")); event_desc.add(feedobj.getstring("event_title")); event_date.add(feedobj.getstring("event_date")); event_place.add(feedobj.getstring("event_place")); event_time.add(feedobj.getstring("event_time")); } listview.setadapter(new datalistadapter(event_date,event_desc,event_place,event_time)); //log.i("event1111", event.tostring()); // log.i("event2222", event2.tostring()); toast.maketext(getactivity(), "dataa"+event_id, 5000).show(); } catch (jsonexception e) { e.printstacktrace(); } }
my problem got data.but when updated json data,the cache data not update.so how can change cache data based on json data.please me in advance :)
set cache header response of server correctly, think need change max-age of header or custom configuration because volley looks @ header stores response in cache, here how dose:
headervalue = headers.get("cache-control"); if (headervalue != null) { hascachecontrol = true; string[] tokens = headervalue.split(","); (int = 0; < tokens.length; i++) { string token = tokens[i].trim(); if (token.equals("no-cache") || token.equals("no-store")) { return null; } else if (token.startswith("max-age=")) { try { maxage = long.parselong(token.substring(8)); } catch (exception e) { } } else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) { maxage = 0; } } } headervalue = headers.get("expires"); if (headervalue != null) { serverexpires = parsedateasepoch(headervalue); } serveretag = headers.get("etag"); // cache-control takes precedence on expires header, if both exist , expires // more restrictive. if (hascachecontrol) { softexpire = + maxage * 1000; } else if (serverdate > 0 && serverexpires >= serverdate) { // default semantic expire header in http specification softexpire. softexpire = + (serverexpires - serverdate); } cache.entry entry = new cache.entry(); entry.data = response.data; entry.etag = serveretag; entry.softttl = softexpire; entry.ttl = entry.softttl; entry.serverdate = serverdate; entry.responseheaders = headers;
Comments
Post a Comment