Android Volley obtains the cached data on the disk, androidvolley
After learning, we know that Volley's architecture is as follows:
From the architecture, we can see that volley has a cache mechanism. When the data cache cannot be found or the data cache expires, new data will be obtained online. Volley has a caching mechanism, which not only caches images by default, but also caches Json data. Through the mobile file management software, we found the Volley cache address:/data/software package/cache/volley directory.
How can I obtain the data in the Volley cache after obtaining the data cache through the Internet? I searched Baidu for a whole day without explaining how to obtain the latest data. Finally, relevant information is found in stack overflow.
In the RequestQueue class, a sub-function getCache () can return to the Cache instance. by calling the get (url) function in the modified instance, you can check whether Cache data is stored on the mobile phone disk, the member variable data stores the cached data content. That is: queue. getCache (). get (url). data
Therefore, you can use the following statement to obtain the cached data or the latest data from the server.
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 will not be able to obtain the latest data, but will retrieve the cached data from the cache.
To this end, I am a stupid method: To determine whether the Network is available, if available, data is updated, and when the network is unavailable, cache data is used.
Context context = getActivity (). getApplicationContext (); if (! IsNetworkAvailable (context) {getFromDiskCache (url); // if there is no network, the cached data will be retrieved} else {// update data from the Internet if there is a network //...... (Omitted )}
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 obtain cached data (using 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 );//...... (Omitted operation)} catch (JSONException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} else {Log. d (TAG, "No cached data ");}}
In fact, when the server does not respond, we can also call the getFromDiskCache () function to retrieve the cached data. Add the corresponding statement to public void onErrorResponse (VolleyError error, do not expand here.
In fact, this is a stupid method. It is reasonable to request to the server to check whether there is any data update. If yes, update the data (that is, the server determines whether the cache is available), but you do not know how to complete it yet, wait and try again.
Reference link:
Http://stackoverflow.com/questions/21902063/volley-how-to-cache-loaded-data-to-disk-and-load-them-when-no-connection-avail