younger brother is very lazy, do not like to write these summary, mainly is limited expression capacity, if you want to exchange volley related, you can see my githubVolley
we all learn to progress together
1. Add URL parameter in volley and form request, need to stitch string yourself, or override Geturl method
StringBuilder sb = new StringBuilder (); if (!url.contains ("?")) { url = url + "?"; } else { if (!url.endswith ("?")) { url = url + ' & '; } } iterator<string> iterotor = Paramsmap.keyset (). Iterator (); while (Iterotor.hasnext ()) { string key = (string) iterotor.next (); try { sb.append (key). Append ("="). Append (Urlencoder.encode (Paramsmap.get (key), "Utf-8"). Append ("&"); } catch (Unsupportedencodingexception e) { e.printstacktrace (); } } if (Sb.lastindexof ("&") = = Sb.length ()-1) { Sb.deletecharat (sb.length ()-1); }
Add parameter override Requst Getparams method in post, or customize request
@Override protected Map<string, string> Getparams () throws Authfailureerror { Paramsengine engine = new Paramsengine (mcontext); map<string, string> p = engine.generaterequestparams (paramsmap); return p; }
2. Volley loading the local image volley itself only handles network requests and caches network requests. However, the schema of the request and response also applies to asynchronously loading local and local files. All network requests were taken to basicnetwork. It is the core of the volley, in Performrequest this method, all request is converted to the corresponding response, to the Networkdispatcher to dispatch. We only need to judge the URL of the request. If it is/or file beginning, all as local file processing, using the local parsing stream to put back to response, while the request in the SHOULDCAHCE set to False, because the local file already exists locally, no longer need to cache to volley cache file, The code is as follows:
String urlstring = Request.geturl (); if (Urlstring.trim (). toLowerCase (). StartsWith ("file")) { try { //If a local picture is loaded and is not placed in the Disk-cache directory Request.setshouldcache (false); return new Networkresponse (Getfromfile (New URI (urlstring). GetPath ())); } catch (URISyntaxException e) { e.printstacktrace (); } } else if (Urlstring.trim (). toLowerCase (). StartsWith ("/")) { Request.setshouldcache (false); return new Networkresponse (Getfromfile (urlstring));
The Getfromfile method, which is to convert the file into a byte array, can refer to the method of converting entity in volley
Private byte[] Getfromfile (String urlstring) throws IOException {File File = new file (urlstring); Poolingbytearrayoutputstream bytes = new Poolingbytearrayoutputstream (mpool, (int) file.length ()); byte[] buffer = NULL; Gets the input stream of the file inputstream in = null; try {buffer = MPOOL.GETBUF (1024); in = new FileInputStream (new File (urlstring)); int count; while ((count = in.read (buffer))! =-1) {bytes.write (buffer, 0, count); } return Bytes.tobytearray (); } finally {if (in! = null) {try {in.close (); } catch (IOException e) {e.printstacktrace (); }} mpool.returnbuf (buffer); try {bytes.close (); } catch (IOException e) {e.printstacktrace (); } } }
Use Bytearraypool here to avoid repeated application of byte[] array space
3. Diskbasedcache initialization is very slow in volley. Initialize method, the traversal of all the cache files, read the header information into the map, when the cache file very much, Google provides the initialization of implementation, consuming more than 10s of time!!! I don't know if this is a bug. That is, each time you newrequestqueue, you need to wait 10 seconds to start processing the request. If that's the case, the framework won't work. I haven't found a good way at the moment. There are two optimization schemes. (1) Someone mentioned the problem in StackOverflow, and then he gave the solution. In the initialize, give fileoutputstream in a layer of bufferedoutputstream. I have tested, 10 times times the speed of ascension. The basic ability to control the initialization time is 1-3s. (2) Early initialization. Each time newrequestqueue takes so long to initialize, but in actual use, do we need to be so many requestqueue of new? As you can see from the source code, each requestqueue actually initiates 5 loop threads to dispatch various requests. Four of these are used to handle network requests and one to handle cache requests. For an app that's enough, we don't need to start a requestqueue on each page. So, I set the Requestqueue as a singleton and initialize it in application so that we can avoid initialize being called repeatedly, and prepare the data beforehand before the interface comes out.
Android's volley framework experience