Volley as a 2013 Google launched a web-based open-source framework for Android devices, has been widely used, the advantages are extensible, caching, dispatcher and so on. Below we simply based on the order of the call, first talk about exactly how the internal operation of the volley, the use of what design patterns, the main class has what is the use and what is the defect.
This article first analyzes the internal flow of volley.
First, when we use volley, we first call the
Requestqueue Mqueue = Volley.newrequestqueue (this);
This part generates a requestqueue, how does the interior go? The overloaded function is called first, and then the following code is overloaded:
public static Requestqueue Newrequestqueue (context context, httpstack stack, int maxdiskcachebytes)
The approach to this will generate a real requestqueue queue, and this method internally does the following things:
1) Generate a file, this file is a cache file that holds volley access to the data for caching use:
File Cachedir = new file (Context.getcachedir (), "volley");//name is volley by default
2) Set useragent, this useragent in the HTTP request head storage, when httpstack use HttpClient (sdk<9), will use this useragent information.
try { String network = Context.getpackagename (); PackageInfo queue = Context.getpackagemanager (). Getpackageinfo (network, 0); useragent = Network + "/" + Queue.versioncode; } catch (Namenotfoundexception var7) { ; }
3) generate the use of the Httpstack, the Httpstack is actually the final manipulation of the request to access the data class, if the call Volley.newrequest () passed in this then use the incoming httpstack, if not passed the SDK Use Hurlstack when >=9, otherwise use httpclient (why?)
if (stack = = null) { if (VERSION. Sdk_int >= 9) { stack = new Hurlstack (); } else { stack = new Httpclientstack (Androidhttpclient.newinstanc E (useragent)); } }
When we generated the requestqueue, we only passed in the context, so httpstack used the default. If you want to write one, you can implement Httpstack and then generate requestqueue when you pass in the httpstack you write.
4) Instantiate the network object, which is also very critical for a class that is networked, and then passes in the previous step of the Httpstack object.
Basicnetwork network1 = new Basicnetwork ((httpstack) stack);
5) Generate the Requestqueue object, specify the maximum cache value if the maximum cache value is specified when constructing requestqueue, otherwise use the default cache value of 5MB
Requestqueue queue1; if (maxdiskcachebytes <=-1) { queue1 = new Requestqueue (new Diskbasedcache (Cachedir), NETWORK1); } else { queue1 = new Requestqueue (new Diskbasedcache (Cachedir, maxdiskcachebytes), NETWORK1); }
6) Call the Queue.start () method, and then return the generated Requestqueue
Queue1.start (); return queue1;
The above is volley.newrequest (context context) of the internal execution process, volley generated Requestqueue objects exposed four methods, respectively:
Requestqueue Newrequestqueue (context context)//Do not specify Httpstack and cache maximum values with the default Requestqueue newrequestqueue (context context, Httpstack stack)//Specifies httpstack does not specify the maximum cache value Requestqueue Newrequestqueue (context context, int maxdiskcachebytes)// Do not specify Httpstack, specify cache Maximum value Newrequestqueue (context context, httpstack stack, int maxdiskcachebytes)//both specified
No matter what method we use to construct the Requestqueue, we will eventually go to the last one, and do 6 things in the last method.
1) Generate cache file
2) specify UserAgent (sdk<9)
3) Instantiation of Httpstack
4) Instantiate network (Basicnetwok)
5) Instantiation of Requestqueue
6) Queue.start () and return to Requestqueue.
So what is the general wording of the request when we use it, and what happens inside?
What is the role of httpstack,network,requetqueue, and what does Queue.start () do? It's a follow-up.
http://blog.csdn.net/szxgg/article/details/51346077
Volley source of the call process and various functions