In the Android Volley analysis (a)-the structure of the main analysis of the volley basic components and framework structure, the components are mainly defined interfaces, that is to say we can implement these interfaces to customize their own version of the volley, such as network, Cache, Request and so on. Android volley has done these things under Com.android.volley.toolbox, so let's look at these specific implementations
First look at an example of volley use
Final TextView Mtextview = (TextView) Findviewbyid (R.id.text);.//Instantiate the Requestqueue.requestqueue queue = Vol Ley.newrequestqueue (this); String url = "http://www.google.com";//Request A string response from the provided URL. Stringrequest stringrequest = new Stringrequest (Request.Method.GET, URL, new Response.listener () { @Override Public void Onresponse (string response) { //Display The first characters of the response String. Mtextview.settext ("Response is:" + response.substring (0,500));} , New Response.errorlistener () { @Override Public void Onerrorresponse (volleyerror error) { Mtextview.settext ("That didn ' t work!"); }); /ADD the request to the RequestQueue.queue.add (stringrequest);
This is an example of an official volley tutorial, using volley steps
1, Volley.newrequestqueue ();
2. New Request ();
3, Queue.add (request);
Basic components are initialized when Newrequestqueue
public static Requestqueue Newrequestqueue (context context, Httpstack stack) {file Cachedir = new File (context . Getcachedir (), default_cache_dir); String useragent = "volley/0"; try {String PackageName = Context.getpackagename (); PackageInfo info = Context.getpackagemanager (). Getpackageinfo (PackageName, 0); useragent = PackageName + "/" + Info.versioncode; } catch (Namenotfoundexception e) {} if (stack = = null) {if (Build.VERSION.SDK_INT >= 9) { stack = new Hurlstack (); } else {//Prior to Gingerbread, httpurlconnection is unreliable. see:http://android-developers.blogspot.com/2011/09/androids-http-clients.html stack = new HttpClientSta CK (Androidhttpclient.newinstance (useragent)); }} Network Network = new Basicnetwork (stack); Requestqueue queue = new Requestqueue (New Diskbasedcache (cACHEDIR), network); Queue.start (); return queue; }
1, Basicnetwork
Basicnetwork is the implementation of the network interface, while also extracting the networking operations to form Httpstack, because the Internet operation before Android 2.3 recommended the use of httpclient, After 2.3 Android made an optimization recommendation using HttpURLConnection.
2, Diskbasedcache
Diskbasedcache is the cache pair implementation that is used for hard disk caching, which is to save the data as a specific file.
Combined with LRUCache try to summarize the general method of caching:
Mapping structure--LINKEDHASHMAP, the internal implementation of the LRU algorithm sequencing, you can directly use
put--storage, increase capacity count, determine if maximum capacity exceeded, delete least used
Get--key to entry mapping
remove--deleted, capacity count decreased
3, Imageloader
Imageloader is a picture loading class that encapsulates the request, provides a memory cache mechanism, batch processing, and so on, makes the picture loading easier to use, and looks at its usage:
Mimageloader = new Imageloader (Mrequestqueue, new Imageloader.imagecache () { private final lrucache<string , bitmap> cache = new lrucache<string, bitmap> (a); @Override public Bitmap getbitmap (String url) { return cache.get (URL); } @Override public void putbitmap (String url, Bitmap Bitmap) { cache.put (URL, Bitmap); }
Mimageloader.get (Image_url, Imageloader.getimagelistener (Mimageview, r.drawable.def_image, R.drawable.err_ image));
(Sample source Making a standard Request)
The
Memory cache recommends the use of LRUCache, which requires implementation of the Imageloader.imagecache interface. The load of the picture is implemented by the Imageloader.get () method, and
Public Imagecontainer Get (String requesturl, Imagelistener imagelistener, int maxWidth, int maxheight) { Only fulfill requests this were initiated from the main thread. Throwifnotonmainthread (); Final String CacheKey = Getcachekey (Requesturl, MaxWidth, maxheight); Try to look up the request in the cache of the remote images. Bitmap Cachedbitmap = Mcache.getbitmap (CacheKey); if (cachedbitmap! = null) {//Return the cached bitmap. Imagecontainer container = new Imagecontainer (cachedbitmap, requesturl, NULL, NULL); Imagelistener.onresponse (container, true); return container; }//The bitmap didn't exist in the cache, fetch it! Imagecontainer Imagecontainer = new Imagecontainer (null, Requesturl, CacheKey, Imagelistener); Update the caller to let them know this they should use the default bitmap. Imagelistener.onresponse (Imagecontainer, true); Check to see if a request is already in-flight. Batchedimagerequest request = Minflightrequests.get (CacheKey); if (Request! = NULL) {//If it is, add this request to the list of listeners. Request.addcontainer (Imagecontainer); return imagecontainer; }//The request is not a already in flight. Send the new request to the network and//track it. request<?> newrequest = new Imagerequest (Requesturl, New listener<bitmap> () {@Overr IDE public void Onresponse (Bitmap response) {ongetimagesuccess (CacheKey, response); }}, MaxWidth, MaxHeight, config.rgb_565, New Errorlistener () {@Overrid e public void Onerrorresponse (Volleyerror error) {ongetimageerror (CacheKey, error); } }); Mrequestqueue.add (newrequest); Minflightrequests.put (CacheKey, New Batchedimagerequest (Newrequest, Imagecontainer)); return imagecontainer; }
First get a cachekey, this is the uniform rule
private static string Getcachekey (string url, int maxWidth, int maxheight) { return new StringBuilder (Url.length () + 1 2). Append ("#W"). Append (maxWidth). Append ("#H"). Append (MaxHeight). Append (URL). toString ();
Then look in the cache caches, not found to say the request added to the batch task Bitmapimagerequest, there are two classes, Imagecontainer and bitmapimagerequest:
Imagecontainer is a wrapper class that contains bitmap, URLs, CacheKey, and listener, which will bitmap back through listener when obtained bitmap;
Bitmapimagerequest is to put together the same URL request, so that you can only request the network once, return the results and then set to the target, so as to reduce the network request for the purpose.
Android Volley Analysis (ii)--implementation