Volley源码分析

想必大家都对Volley已经很熟悉了吧,google工程师出的这个网络框架代码写的是真好,值得我们大家去学习,Volley之所以一出来就受到广大程序员的欢迎,是因为它可以用非常简单的方法来发送Http请求,并且处理服务器返回的数据,而且是直接返回到主线程,这样是不是用起来很爽。

很早之前出的网络框架,也很早就研究了,就是一直没有写,现在抽空写出来分享给需要的人,写的不好的地方请见谅。

Volley的简单用法请看这个大神的博主Volley的简单使用

Volley的框架图
1734948-1f5268ebb67c2be2.png

volley的整个流程其实很简单:得到请求添加到缓存请求队列中,然后经过一系列的判断,是否缓存中已经有此请求,有取出,没有放入网络请求队列中 ,最后都是将得到的结果返回到主线程中。

Volley整个框架是一个典型的生产消费者模式 一个消费者(CacheDispatcher) 也可以是下一个消费者(NetworkDispatcher)的生产者。

接下来我们就从整个流程开始说
1.RequestQueue requestQueue = Volley.newRequestQueue(this);

顾名思义,这一步创建了一个requestQueue请求队列。Volley.newRequestQueue(this),别看这个短短的一段代码,其背后是做了整个volley的准备工作。(后面我们会细讲)

2.Request

 StringRequest request = new StringRequest("https://www.baidu.com/", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
public abstract class Request<T> implements Comparable<Request<T>>

这个Request是个泛型其中<T> 是你的返回类型。request实现Comparable这意味着是两个request之间是可以比较。
Volley官方自带了一些自己的request

  • StringRequest
  • ImageRequest
  • JsonRequest
  • JsonArrayRequest
  • JsonObjectRequest
  • ClearCacheRequest
    我们也可以根据需求自己创建Request,在自定义Request的时候我们需要我们实现两个抽象类:
void deliverResponse(T response)
Response<T> parseNetworkResponse(NetworkResponse response)
  1. 第一个方法是用来把解析的结果如何传递,一般都是用volley自带的:Response.Listener<T> 回调接口传递。
    2.第二个方法是用来把NetworkResponse解析成自己想的得到的结果,比如StringRequest,是转换成Sting,ImageRequest是转换成Bitmap。然后传给第一个方法deliverResponse。

3. requestQueue.add(request);

接下来说一些这个RequestQueue

public RequestQueue(Cache cache, Network network, int threadPoolSize,
            ResponseDelivery delivery) {
        mCache = cache;
        mNetwork = network;
        mDispatchers = new NetworkDispatcher[threadPoolSize];
        mDelivery = delivery;
    } 

创建RequestQueue需要的参数

  • Cache:Volley使用的持久化磁盘缓存:DiskBasedCache(cacheDir)
  • Network:进行发送网络请求的工具。
  • int threadPoolsize:这个是NetworkDispatcher这个线程的数量。
  • ResponseDelivery:用于传递请求结果的类。
RequestQueue的主要结构:
  • Map<String, Queue<Request<?>>> mWaitingRequests :等待请求的集合,一些重复的请求存放区域,
  • Set<Request<?>> mCurrentRequests:当前请求的集合,包括了所有正在请求的,或者是等待请求的都放在这个集合中
  • PriorityBlockingQueue<Request<?>> mCacheQueue:缓存队列。(每一个请求在进入队列中都会先进入这个缓存队列)
  • PriorityBlockingQueue<Request<?>> mNetworkQueue:网络请求队列。
  • AtomicInteger mSequenceGenerator :这个类是给每个Request 生成序列号的。
然后我们来看一下add这个方法,都做了哪些事情
 /**
     * Adds a Request to the dispatch queue.
     * @param request The request to service
     * @return The passed-in 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.
A:
        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;
        }
B:
        // 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;
        }
    }

我在上面这段源码中标注了一下A,和B,我们可以看到使用了两个同步锁来往m
CurrentRequests,和mWaitingRequests 存放请求。
A中所有进来的请求都添加进去了。
B中我们看到mWaitingRequests 的Key是Request的key,Value是request。

我们来详细讲解一下创建RequestQueue

Volley.newRequestQueue();
这个方法首先会考虑你当前的android的api是否>=9
如果大于等于9(3.0) Volley 会选择使用HttpUrlConnectiion,小于9.0 选择使用的是HttpClient。Volley分别创建了HurlStack,和HttpClientStack,来封装这两个网络请求方法。Volley的RequestQueue只要持有Network接口就可以了。所有的网络操作都在HttpStack的子类中进行的。而NetWork接口中的performRequest方法调用了HttpSackt.performRequest方法就得到结果了。

queue.start();

  /**
     * Starts the dispatchers in this queue.
     */
    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();
        }
    }

从代码中可以看出,这个start方法是开启了所有的Dispatcher(一个缓存线程,四个网络请求线程),stop方法是停止线程的方法。

那么来看一下这两个Dispatcher:

CacheDispatcher

创建CacheDispatcher所需要的参数:

  • BlockingQueue<Request<?>> cacheQueue :缓存队列;
  • BlockingQueue<Request<?>> networkQueue: 网络请求队列;
  • Cache cache:磁盘缓存队列;
  • ResponseDelivery delivery:new ExecutorDelivery(new Handler(Looper.getMainLooper())); 这个类是调度请求结果到主线程的,可以看出在创建的时候,创建了一个handler,然后通过handler把结果发送到主线程,这就是为什么Volley得到的结果在主线程的原因(后面会详细讲解)。创建这个对象是在RequestQueue的构造方法中创建的。

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();

        while (true) {
            try {
                // Get a request from the cache triage queue, blocking until
                // at least one is available.
A:
                final Request<?> request = mCacheQueue.take();
                request.addMarker("cache-queue-take");
B:
                // If the request has been canceled, don't bother dispatching it.
                if (request.isCanceled()) {
                    request.finish("cache-discard-canceled");
                    continue;
                }
C:
                // 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;
                }
D:
                // 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");
E:
                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;
F:
                    // Post the intermediate response back to the user and have
                    // the delivery then forward the request along to the network.
                    mDelivery.postResponse(request, response, new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mNetworkQueue.put(request);
                            } catch (InterruptedException e) {
                                // Not much we can do about this.
                            }
                        }
                    });
                }

            } catch (InterruptedException e) {
                // We may have been interrupted because it was time to quit.
                if (mQuit) {
                    return;
                }
                continue;
            }
        }
    }

我把整个代码分成了几段:

  • A:可以看出来CacheDispatcher会一直While(true)的运行,如果CacheQueue里面没有Request的话,会被CacheQueue.take()阻塞;
  • B:判断Request是否已经取消了。
  • C: 获得这个Reuqest在缓存中的结果。判断是否为空,为空就把请求放到网络请求队列里面。
  • D:判断entry是否已经失效了,失效放到网络请求队列。
    -E:判断entry需不需要从新请求,不需要的话就直接用过Delivery返回到主线程。需要的话,把它放到网络请求线程。

NetworkDispatcher

这个和CacheDispatcher是差不多的就不详细写了,就是通过mNetwork.performRequest(request);进行网络请求得到结果,通过mDelivery.postResponse(request, response);发送到主线程。

ExecutorDelivery

ExecutorDeliver 实现了ResponseDelivery接口。主要工作就是把得到的结果传递到主线程。

我们来看一下他的构造方法:

 /**
     * Creates a new response delivery interface.
     * @param handler {@link Handler} to post responses on
     */
    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);
            }
        };
    }

构造方法中创建了线程池来管理线程
Eexecutor 是一个异步执行框架,将任务的提交和执行解耦,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务的线程相当于消费者,用Runnable来表示任务。

在上面的CacheDispatcher和,NetWorkDispatcher中用到了这两个方法
A : mDelivery.postResponse(request, response);
B: mDelivery.postResponse(request, response, new Runnable() );
这两个方法:
A:这个方法其实调用的是B方法:

  @Override
    public void postResponse(Request<?> request, Response<?> response) {
        postResponse(request, response, null);
    }

B:

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

这个方法中调用了Delivery的execute方法,此方法的构造方法传入的是Runnable,,在runnable的run方法中调用了Reuqest的 mRequest.deliverResponse(mResponse.result);最后吧结果传到了主线程。

整个Volley的工作流程和源码分析就结束了。整个Volley是基于生产者和消费者模式。层次清晰,值得借鉴。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。