Volley的源码分析

Volley是2013年Google I/O大会上推出了一个新的网络通信框架。因为Volley集成了AsyncHttpClient和Universal-Image-Loader的优点集于了一身,能使网络通信更快,更简单,也可以像Universal-Image-Loader一样轻松加载网络上的图片,所以深受广大开发者的喜爱。虽然volley现在已经过时,已经有其他如okhttp等优秀开源框架可以代替,但是volley的编程思想和源码还是有很多东西值得我们学习。
优点:
1. 适合进行通信频繁的网络操作
2. volley是开源的,可以根据自己的需求进行扩展封装,例如:自定义XmlRequest,GsonRequest可扩展性很强。

缺点:
1. 对于大数据量的网络操作,比如文件下载,volley则表现的不好。
2. volley使用的是使用的是httpclient、HttpURLConnection,但是6.0以后不在支持httpclient

Volley的工作流程图,如下图所示。
这里写图片描述

RequestQueue.add添加一条网络请求,首先这个request会被添加到到cacheQueue对列当中,如果缓存中有相应的缓存,则读取—>解析—>回调给主线程,如果缓存中没有,则添加到网络请求对列当中,则http request—>paresed—>cache write—>回调给主线程。
这是整个volley请求的思路,我们沿着这条主线进行阅读源码。

  • 1.要把http request添加到requestQueue中,就需要先创建requestQueue,下面就从Volley.newRequestQueue(context)开始阅读
 public static RequestQueue newRequestQueue(Context context, HttpStack stack, int maxDiskCacheBytes) {
        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;
        if (maxDiskCacheBytes <= -1)
        {
            // No maximum size specified
            queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
        }
        else
        {
            // Disk cache size specified
            queue = new RequestQueue(new DiskBasedCache(cacheDir, maxDiskCacheBytes), network);
        }

        queue.start();

        return queue;
    }

首先获取网络缓存路径,context.getCacheDir()获取的路径/data/data/PackageName/cache目录,其次应用的一些信息,第10行开始创建http栈,如果手机系统版本大于9即Android 2.3,则用HurlStack,版本小于9则用HttpClientStack,打大HurlStack在performRequest中可以看到HttpURLConnection connection = openConnection(parsedUrl, request); 而HttpClientStack则用的是HttpClient进行网络通讯的。最后创建RequestQueue网络请求对列,返回queue 。

总结:在创建requestQueue时,当手机系统版本大于9用的是HttpURLConnection ,小于9则是HttpClient,现在基本上没有版本是9的系统的手机了。所以HttpClient可以忽略。
但是为什么现在高版本,或者说主流都是用HttpURLConnection进行网络通讯呢?

  1. HttpClient:基于apache,是个重量级的程序而且API数量很多,在Android2.3之前httpClient的bug少,而且实现比较稳定。HttpURLConnection存在bug较多。
  2. HttpURLConnection:基于Java的轻量级,在Android2.3以后HttpURLConnection修复完善,存在很少的bug,多用途,api少,简单,具有压缩和缓存机制可以有效的减少网络访问的流量,在提高速度和省电方面起到很大的作用,另外,在Android6.0以后HttpClient库 已经被移除,因而非常适用于Android项目。

    下面看一下queue.start()的源码

 public void start() {
        stop();  // Make sure any currently running dispatchers are stopped.
        // Create the cache dispatcher and start it.
        mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
        mCacheDispatcher.start();

        // Create network dispatchers (and corresponding threads) up to the pool size.
        for (int i = 0; i < mDispatchers.length; i++) {
            NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork,
                    mCache, mDelivery);
            mDispatchers[i] = networkDispatcher;
            networkDispatcher.start();
        }
    }

首先在requestQueue启动的时候,要停止所有的正在执行的线程,CacheDispatcher(是缓存线程)和NetworkDispatcher (网络请求线程)都是继承与Thread , Dispatchers的length默认是DEFAULT_NETWORK_THREAD_POOL_SIZE =4。也就是说当requestQueue启动的时候有5个线程启动,1个缓存线程,4个网络请求线程

  • 2.获取了requestQueue之后就是添加网络请求Request。

下面看一下requestQueue.add(request)的源码:

  public <T> Request<T> add(Request<T> request) {
        // Tag the request as belonging to this queue and add it to the set of current requests.
        request.setRequestQueue(this);
        synchronized (mCurrentRequests) {
            mCurrentRequests.add(request);
        }

        // Process requests in the order they are added.
        request.setSequence(getSequenceNumber());
        request.addMarker("add-to-queue");

        // If the request is uncacheable, skip the cache queue and go straight to the network.
        if (!request.shouldCache()) {
            mNetworkQueue.add(request);
            return request;
        }

        // Insert request into stage if there's already a request with the same cache key in flight.
        synchronized (mWaitingRequests) {
            String cacheKey = request.getCacheKey();
            if (mWaitingRequests.containsKey(cacheKey)) {
                // There is already a request in flight. Queue up.
                Queue<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey);
                if (stagedRequests == null) {
                    stagedRequests = new LinkedList<Request<?>>();
                }
                stagedRequests.add(request);
                mWaitingRequests.put(cacheKey, stagedRequests);
                if (VolleyLog.DEBUG) {
                    VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
                }
            } else {
                // Insert 'null' queue for this cacheKey, indicating there is now a request in
                // flight.
                mWaitingRequests.put(cacheKey, null);
                mCacheQueue.add(request);
            }
            return request;
        }
    }

首先将request请求添加到mCurrentRequests当中,mCurrentRequests是保存当前需要处理的所以request。
接下里看如果request.shouldCache()设置不缓存,则直接将request添加到网络请求队列中,然后返回。
然后判断该请求是否有相同的请求正在被处理,如果有则加入mWaitingRequests;如果没有,则加入mWaitingRequests.put(cacheKey, null)和mCacheQueue.add(request)。

  • 3.接下来看一下缓存线程的工作原理,查看CacheDispatcher.run()
 public void run() {
        if (DEBUG) VolleyLog.v("start new dispatcher");
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

        // Make a blocking call to initialize the cache.
        mCache.initialize();

        Request<?> request;
        while (true) {
            // release previous request object to avoid leaking request object when mQueue is drained.
            request = null;
            try {
                // Take a request from the queue.
                request = mCacheQueue.take();
            } catch (InterruptedException e) {
                // We may have been interrupted because it was time to quit.
                if (mQuit) {
                    return;
                }
                continue;
            }
            try {
                request.addMarker("cache-queue-take");

                // If the request has been canceled, don't bother dispatching it.
                if (request.isCanceled()) {
                    request.finish("cache-discard-canceled");
                    continue;
                }

                // Attempt to retrieve this item from cache.
                Cache.Entry entry = mCache.get(request.getCacheKey());
                if (entry == null) {
                    request.addMarker("cache-miss");
                    // Cache miss; send off to the network dispatcher.
                    mNetworkQueue.put(request);
                    continue;
                }

                // If it is completely expired, just send it to the network.
                if (entry.isExpired()) {
                    request.addMarker("cache-hit-expired");
                    request.setCacheEntry(entry);
                    mNetworkQueue.put(request);
                    continue;
                }

                // We have a cache hit; parse its data for delivery back to the request.
                request.addMarker("cache-hit");
                Response<?> response = request.parseNetworkResponse(
                        new NetworkResponse(entry.data, entry.responseHeaders));
                request.addMarker("cache-hit-parsed");

                if (!entry.refreshNeeded()) {
                    // Completely unexpired cache hit. Just deliver the response.
                    mDelivery.postResponse(request, response);
                } else {
                    // Soft-expired cache hit. We can deliver the cached response,
                    // but we need to also send the request to the network for
                    // refreshing.
                    request.addMarker("cache-hit-refresh-needed");
                    request.setCacheEntry(entry);

                    // Mark the response as intermediate.
                    response.intermediate = true;

                    // Post the intermediate response back to the user and have
                    // the delivery then forward the request along to the network.
                    final Request<?> finalRequest = request;
                    mDelivery.postResponse(request, response, new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mNetworkQueue.put(finalRequest);
                            } catch (InterruptedException e) {
                                // Not much we can do about this.
                            }
                        }
                    });
                }
            } catch (Exception e) {
                VolleyLog.e(e, "Unhandled exception %s", e.toString());
            }
        }
    }

首先设置进入while(true)中,request = mCacheQueue.take()中获取request请求对象,
如果request.isCanceled()已经退出则结束,继续执行下一个request,
获取缓存Cache.Entry entry = mCache.get(request.getCacheKey());
如果entry==null,则将该请求添加到网络请求中mNetworkQueue.put(request);
如果entry.isExpired()失效,则将该请求添加到网络请求中mNetworkQueue.put(request);
接下来就是 parseNetworkResponse()方法来对数据进行解析。parseNetworkResponse()必须有子类去实现。

总结:缓存线程中获取request,如果没有取消,则去获取缓存数据,如果缓存不存在或者过期失效,都会加入到网络请求队列当中,如果存在缓存数据在解析数据parseNetworkResponse(response),返回给主线程中deliverResponse(response)。

  • 4.看一下NetworkDispatcher.run()方法
 public void run() {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
        Request<?> request;
        while (true) {
            long startTimeMs = SystemClock.elapsedRealtime();
            // release previous request object to avoid leaking request object when mQueue is drained.
            request = null;
            try {
                // Take a request from the queue.
                request = mQueue.take();
            } catch (InterruptedException e) {
                // We may have been interrupted because it was time to quit.
                if (mQuit) {
                    return;
                }
                continue;
            }

            try {
                request.addMarker("network-queue-take");

                // If the request was cancelled already, do not perform the
                // network request.
                if (request.isCanceled()) {
                    request.finish("network-discard-cancelled");
                    continue;
                }

                addTrafficStatsTag(request);

                // Perform the network request.
                NetworkResponse networkResponse = mNetwork.performRequest(request);
                request.addMarker("network-http-complete");

                // If the server returned 304 AND we delivered a response already,
                // we're done -- don't deliver a second identical response.
                if (networkResponse.notModified && request.hasHadResponseDelivered()) {
                    request.finish("not-modified");
                    continue;
                }

                // Parse the response here on the worker thread.
                Response<?> response = request.parseNetworkResponse(networkResponse);
                request.addMarker("network-parse-complete");

                // Write to cache if applicable.
                // TODO: Only update cache metadata instead of entire record for 304s.
                if (request.shouldCache() && response.cacheEntry != null) {
                    mCache.put(request.getCacheKey(), response.cacheEntry);
                    request.addMarker("network-cache-written");
                }

                // Post the response back.
                request.markDelivered();
                mDelivery.postResponse(request, response);
            } catch (VolleyError volleyError) {
                volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
                parseAndDeliverNetworkError(request, volleyError);
            } catch (Exception e) {
                VolleyLog.e(e, "Unhandled exception %s", e.toString());
                VolleyError volleyError = new VolleyError(e);
                volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
                mDelivery.postError(request, volleyError);
            }
        }
    }

从消息队列中获取request,然后request执行mNetwork.performRequest(request)发送网络请求,而Network是一个接口,这里具体的实现是BasicNetwork,mNetwork.performRequest(request)里面主要是http网络请求的底层封装,版本大于9时用httpUrlClient,小于9用httpClient.
在NetworkDispatcher中收到了NetworkResponse这个返回值后,调用Request.parseNetworkResponse()方法来解析NetworkResponse中的数据,接着是是否需要设置缓存(setShouldCache()),需要则存入缓存。
最后调用mDelivery.postResponse(request, response)

  • 看一下mDelivery.postResponse(request, response)的源码

    @Override
    public void postResponse(Request<?> request, Response<?> response, Runnable runnable) {
        request.markDelivered();
        request.addMarker("post-response");
        mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable));
    }

ResponseDeliveryRunnable实现了Runnable接口,ResponseDeliveryRunnable的run()方法

        public void run() {
            // If this request has canceled, finish it and don't deliver.
            if (mRequest.isCanceled()) {
                mRequest.finish("canceled-at-delivery");
                return;
            }

            // Deliver a normal response or error, depending.
            if (mResponse.isSuccess()) {
                mRequest.deliverResponse(mResponse.result);
            } else {
                mRequest.deliverError(mResponse.error);
            }

            // If this is an intermediate response, add a marker, otherwise we're done
            // and the request can be finished.
            if (mResponse.intermediate) {
                mRequest.addMarker("intermediate-response");
            } else {
                mRequest.finish("done");
            }

            // If we have been provided a post-delivery runnable, run it.
            if (mRunnable != null) {
                mRunnable.run();
            }
       }

如果成功则调用mRequest.deliverResponse(result),stringRequest里面的实现是mListener.onResponse(response)回调给成功接口。
进入mResponsePoster.execute()源码看看

 public ExecutorDelivery(final Handler handler) {
        // Make an Executor that just wraps the handler.
        mResponsePoster = new Executor() {
            @Override
            public void execute(Runnable command) {
                handler.post(command);
            }
        };
    }

内部封装的handler.post(runable)完成了数据向主线程的传递。

volley的网络请求思想就是延续开头我们描述的流程走,volley是个优秀的开源框架,具有很好的扩展性,如果想根据自己的需求自定义网络请求格式,只需要继承Request,实现俩个重要的方法就可以了;

  1. parseNetworkResponse(NetworkResponse response),发生在子线程中,解析网络请求数据。
    获取网络请求的数据response.data是以字节的形式存在,我们在此根据需求转换成xmlRequest或者GsonRequest…

  2. deliverResponse(T response)传递返回
    response是子线程获取的数据(缓存或者网络请求的数据)利用handler消息机制回调到主线程。通过接口返回给我们需要的场景。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值