After learning, we know that the architecture of volley is as follows:
From the schema we can see that volley has a caching mechanism, and when the data cache is not found or the data cache expires, the network gets new data. The volley itself has a caching mechanism that not only caches images by default, but also caches JSON data. Through the mobile file management software, we found volley cache address:/data/data/package/cache/volley directory.
So, how do you get the data in the volley cache after the data cache has been obtained on the internet? The search for a whole day on Baidu did not show how to get the latest data. Finally, we found the relevant data in the stack overflow.
There is a child function in the Requestqueue class GetCache () can return to the cache instance, by invoking the Get (URL) function in the instance to see if there is any cached data stored in the phone's disk, and its member variable data holds the cached content. That is: Queue.getcache (). Get (URL). Data
Therefore, we can choose to get the cached data or get the latest data from the server by the following statement.
if (Queue.getcache (). Get (URL)!=null) { //response exists string cachedresponse = new String (Queue.getcache (). Get (URL). data);} else{ //no response Queue.add (stringrequest);}
In fact, this is still flawed, that is, if the server updates the data, then our client has no way to get the latest data, but from the cache to retrieve the cached data.
To this end, I am a more stupid method is: To determine whether the network is available, if available, update the data, when the network is not available, the use of cached data.
Context context = Getactivity (). Getapplicationcontext (); if (!isnetworkavailable (context)) {Getfromdiskcache (URL); If there is no net, then the cache data}else{//network is updated data from the Internet//... (omitted)}
where the isnetworkavailable () function is used to determine whether the network is available:
public static Boolean isnetworkavailable (context context) { try {connectivitymanager manger = (Connectivitymanager) Context . Getsystemservice (context.connectivity_service); Networkinfo info = manger.getactivenetworkinfo (); Return (Info!=null && info.isconnected ()); if (info! = null) { return info.isconnected (); } else{ return false;} } catch (Exception e) { return false;}}
The Getfromdiskcache () function is used to get cached data (take Jsonarray as an example):
private void Getfromdiskcache (string url) {if (Mqueue.getcache (). Get (URL)!=null) {try {string str = new String ( Mqueue.getcache (). Get (URL). data); Jsonarray response = new Jsonarray (str);//... (Omit operation)} catch (Jsonexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} ELSE{LOG.D (TAG, "no cache Data");} }
in fact, when the server is not responding, we can also call the Getfromdiskcache () function to fetch the cached data, add the corresponding statement in public void Onerrorresponse (Volleyerror error) {}, Do not expand here.
In fact, this is a more stupid approach, according to the truth should be to the server request, see if there is data updates, there is the update data (that is, the server determines whether the cache is available) but do not know how to complete, and so on later change it.
Reference Links:
Http://stackoverflow.com/questions/21902063/volley-how-to-cache-loaded-data-to-disk-and-load-them-when-no-connection-avail
Android volley get disk already has cached data