Fast implementation of Android offline cache

Source: Internet
Author: User

Offline cache refers to the network data obtained from the server in the state of the network, such as JSON data cache to the local, in the state of the network to start the app read the local cache data displayed on the interface, the common app (NetEase news, know, etc.) are supported offline caching, which brings a better user experience.

If it is possible to automatically cache the returned JSON data after calling the network interface, it would be nice to call this interface to get the cached JSON data the next time it is in the off-grid state. Volley did that.

Therefore, today this article introduces the use of volley with the data cache, with the Universal-imageloader image cache, to achieve the state of the text display.

Achieve results

How is it implemented?

1. Accessing the network interface using volley

 /** * Get Network data * /    Private void GetData() {Stringrequest stringrequest =NewStringrequest (Request.Method.POST, Test_api,NewResponse.listener<string> () {@Override             Public void Onresponse(String s) {Textview.settext ("Data from the Internet:", T);Try{Jsonobject Jsonobject =NewJsonobject (s); Jsonarray resultlist = Jsonobject.getjsonarray ("Resultlist"); Jsonobject Jsonobject = (org.json.JSONObject) resultlist.opt (0); String head_img = jsonobject.getstring ("Head_img");                Imageloader.getinstance (). DisplayImage (head_img, ImageView); }Catch(Jsonexception e)                {E.printstacktrace (); }            }        },NewResponse.errorlistener () {@Override             Public void Onerrorresponse(Volleyerror volleyerror) {            }        }) {@Override            protectedMap<string, string>Getparams()throwsAuthfailureerror {map<string, string> Map =NewHashmap<string, string> (); Map.put ("Phone","15962203803"); Map.put ("Password","123456");returnMap        }        };    Queue.add (stringrequest); }

When the interface is successfully accessed, volley automatically caches this record in the/data/data/{package Name}/cache/volley folder.

Open the above file, you can find the interface path and the return value are saved in the file.

How do you get the return value of the cache to the interface when the network is disconnected?
Use the GetCache () method provided by Requestqueue to query the cache data for this interface

if (queue.getCache().!=null) {                        String=newString(queue.getCache().get(TEST_API).data);

2. Loading pictures with Universal-imageloader

ImageLoader.getInstance().displayImage(head_img, imageView);
Watch out.

1. Looking at the cache file above, volley only caches the interface path and does not have the incoming parameters of the cache interface, so it is inappropriate to use this method if paging queries are done.
2. During the testing process, it is still found that there are times when the cache data is not available and sometimes it can be obtained. The lazy loading of the code to get the cache can solve this problem effectively.
3. If you consider the expiration policy of the cache, you can use a better asimplecache framework to aid development. Apps that have higher requirements for caching should still use file caches or database caches.

Source code Download

Fast implementation of Android offline cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.