Volley Learning (Volly analysis), volleyvolly
Record your learning experience:
Volley is a network communication library on the android platform, which enables faster, simpler, and more convenient network communication. Volly is suitable for scenarios with small data volumes but frequent communication.
First paste the Volley architecture diagram:
The Volley framework is designed mainly for the following classes: Request (and ImageRequest, StringRequest, JsonReques, etc.), Volly, RequestQueue, queue, DiskBasedCache network, dispatcher, BasicNetWork, HurlStack (HttpClientStack ), byteArrayPool, NetworkResponse, and ExcutorDeliery (written in the order in which the Request is processed from the new Request ).
The following analysis is completed in the order of request creation and processing.
In the Volley framework, Request management is controlled by RequestQueue. When I create a new Request, I first create a RequestQueue object,
RequestQueue = Volley. newRequestQueue (context );
Check what newRequestQueue has done.
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 was unreliable. // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent)); } } Network network = new BasicNetwork(stack); RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network); queue.start(); return queue; }
First, create a cache directory in the default cache file of android. Here, why is the default cache folder selected because it is placed in the default folder of the system? When the apk is deleted, the cache in the folder is also deleted.
After setting userAgent, create HttpStack. Volley creates different stacks based on the system version. If the SDk version is earlier than 9, select HtpClientSack. Otherwise, select HurlStack and Volley.
Before Froyo (2.2), there was a major Bug in HttpURLConnection. Calling the close () function will affect the connection pool, resulting in invalid connection reuse. Therefore, keepAlive needs to be disabled when using HttpURLConnection before Froyo.
In addition, gzip compression is enabled by default in Gingerbread (2.3) HttpURLConnection to improve HTTPS performance. Ice Cream Sandwich (4.0) HttpURLConnection supports request result caching.
In addition, the HttpURLConnection API is relatively simple. Therefore, for Android, we recommend that you use HttpURLConnection after 2.3. We recommend that you use AndroidHttpClient before.
After creating an HttpStack instance, start to create a request queue. There are two parameters in RequestQueue: one is the cache processing class (DiskBasedCache) and the other is the network request processing class (BasicNetwork). After initialization, call queue. start () method, start the thread (this part is analyzed in RequestQueue) and wait for the Request to arrive.