Volley source code analysis framework

Posted by desolator on Thu, 02 Apr 2020 06:29:48 +0200

Analysis of Volley framework

Recently, I am learning the Volley framework and recording my income. On the one hand, it is convenient for me to communicate with you, and on the other hand, it is convenient for me to review in the future. If there is something wrong, please correct it. In the future, we will update and introduce the source code of the important Volly classes.

The above figure shows the basic flow chart of the Volley framework.

1. The entry class of the Volley framework is the Volley class. In the Volley class, call the following methods to determine the network request mode (HttpUrlConnection\DefaultHttpClient), the cache method (DiskBasedCache), and the start of sub thread processing request queue (CacheDispatcher and NetworkDispatcher).

// According to the android version, determine the network request mode, cache method, and start the sub thread to process the request queue
public static RequestQueue newRequestQueue(Context context, BaseHttpStack stack) {
        BasicNetwork network;
        if (stack == null) {
            if (Build.VERSION.SDK_INT >= 9) {
                // SDK is higher than Android API Level 9 (Android 2.2), use HttpURLConnection for network request
                network = new BasicNetwork(new HurlStack());
            } else { // Use DefaultHttpClient to make network request
                String userAgent = "volley/0";
                try {
                    String packageName = context.getPackageName();
                    PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
                    userAgent = packageName + "/" + info.versionCode;
                } catch (NameNotFoundException e) {

                network = new BasicNetwork(
                        new HttpClientStack(AndroidHttpClient.newInstance(userAgent)));
        } else {
            network = new BasicNetwork(stack);
        // Determine the cache method, and start the sub thread to process the request queue
        return newRequestQueue(context, network);
private static RequestQueue newRequestQueue(Context context, Network network) {
        File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
        // Create a request queue and determine the caching method
        RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
        // Enable sub thread to process network request
        return queue;

2. When a new network request is to be initiated, first put the network request into the RequestQueue queue, and then according to the type of network request (described in detail in the following article), CacheDispatcher or NetworkDispatcher (CacheDispatcher and NetworkDispatcher are essentially Thead) will take the request out of the queue for processing. In the process of processing, if necessary Send a network request to the server to get the data, then the network request method (HttpUrlConnection\DefaultHttpClient) created in the Volley class will be used for specific network request work. The result of the network request is finally distributed by ExecutorDelivery.

Topics: network Android SDK