Vollery源码阅读(—)

写在最前面的话:送给还在迷茫看不懂的童鞋,跟着我一步步看,小白也能看懂,从而对总体有一个把握,分析的开始以基本使用为切入点,一步步深刻。java

1. 建立获取 RequestQueue 对象

RequestQueue mQueue = Volley.newRequestQueue(this);
复制代码

开始进入::: Vollery # newRequestQueue()数组

public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
        .....
        if (stack == null) {
            if (VERSION.SDK_INT >= 9) {
                stack = new HurlStack();
            } else {
                stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
            }
        }

        Network network = new BasicNetwork((HttpStack)stack);
        RequestQueue queue = new RequestQueue(new NoCache(), network);
        queue.start();
        return queue;
    }
复制代码

看到我开始罗列代码了,不要慌,还好不长,在能力范围内:缓存

image

刚开始就根据不一样的Android 系统版本,建立不一样的对象,咱们先能够大概了解一下 HurlStackHttpClientStack 是啥?bash

HurlStack.java网络

public class HurlStack implements HttpStack {
      ....
    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) {
            ....
            URL parsedUrl = new URL(url);
            HttpURLConnection connection = this.openConnection(parsedUrl, request); 
           ....
    }
复制代码

HttpClientStack.java函数

public class HttpClientStack implements HttpStack {
    protected final HttpClient mClient;
    ...
    public HttpResponse performRequest(Request<?> request, 
                         Map<String, String> additionalHeaders)  {
          ....
         return this.mClient.execute(httpRequest)
    }
}
复制代码

HttpStack.javaoop

public interface HttpStack {
    HttpResponse performRequest(Request<?> var1, Map<String, String> var2) ;
}
复制代码

看到这里咱们大概明白了,原来是根据不一样的系统版本,肯定最终选择进行的网络请求,那为何大于9 用 HttpUrlConnection 小于9用 HttpClient 呢?在这里不过多介绍了,网上一搜就知道了。ui

Ok,那咱们继续向下走,回到上面的代码,为了方便查看,我从新粘贴一份代码下来:this

public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
        .....
        if (stack == null) {
            if (VERSION.SDK_INT >= 9) {
                stack = new HurlStack();
            } else {
                stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
            }
        }
        //HttpStack 又一次被封装为Network接口类型
        Network network = new BasicNetwork((HttpStack)stack);
        RequestQueue queue = new RequestQueue(new NoCache(), network);
        queue.start();
        return queue;
    }
复制代码

Network.javaurl

public interface Network {
    NetworkResponse performRequest(Request<?> var1) throws VolleyError;
}
复制代码

哦 原来是接口,它的实现类 BasicNetwork BasicNetwork.java

public class BasicNetwork implements Network {
    //将网络请求的实例传入,方便后面的调用
  public BasicNetwork(HttpStack httpStack) {
        this(httpStack, new ByteArrayPool(DEFAULT_POOL_SIZE));
    }
   ....
}
复制代码

目前而止,那么它们之间的关系是啥样的呢,我画了一张图:

image.png
很清晰吧,目前咱们先不考虑这个 BasicNetwork 类中干了什么,咱们先根据代码的思路一步步向下走,保证咱们总体主干不变,避免陷入只见树木不见森林的局势。

Ok,咱们再次回到原来的代码:

public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
        .....
        if (stack == null) {
            if (VERSION.SDK_INT >= 9) {
                stack = new HurlStack();
            } else {
                stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
            }
        }
        //HttpStack 又一次被封装为Network接口类型,
       //建立BasicNetwork调用了构造方法是一个参数的
        Network network = new BasicNetwork((HttpStack)stack);
      //到这里了...........
      //建立一个 请求队列 RequestQueue,而且在构造函数中,传入了两个
     //参数,好,咱们接下来就要去RequestQueue.java类中看一眼了。
        RequestQueue queue = new RequestQueue(new NoCache(), network);
        queue.start();
        return queue;
    }
复制代码

RequestQueue.java

public RequestQueue(Cache cache, Network network) {
        this(cache, network, 2);
    }
public RequestQueue(Cache cache, Network network, int threadPoolSize) {
        this(cache, network, threadPoolSize, new ExecutorDelivery(new Handler(Looper.getMainLooper())));
    }
public RequestQueue(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery) {
        this.mSequenceGenerator = new AtomicInteger();
        this.mWaitingRequests = new HashMap();
        this.mCurrentRequests = new HashSet();
        this.mCacheQueue = new PriorityBlockingQueue();
        this.mNetworkQueue = new PriorityBlockingQueue();
        this.mCache = cache;
        this.mNetwork = network;
        this.mDispatchers = new NetworkDispatcher[threadPoolSize];
        this.mDelivery = delivery;
    }
复制代码

在构造方法中,传入一个Cache 对象,network 对象,默认初始化一个threadPoolSize = 2,还有一系列初始化操做.

Ok,再次返回咱们以前的代码:

....
   RequestQueue queue = new RequestQueue(new NoCache(), network);
  queue.start();
复制代码

RequestQueue#start() 方法了:

public void start() {
        this.stop();
        this.mCacheDispatcher = new CacheDispatcher(this.mCacheQueue, this.mNetworkQueue, this.mCache, this.mDelivery);
        this.mCacheDispatcher.start();

        for(int i = 0; i < this.mDispatchers.length; ++i) {
            NetworkDispatcher networkDispatcher = new NetworkDispatcher(this.mNetworkQueue, this.mNetwork, this.mCache, this.mDelivery);
            this.mDispatchers[i] = networkDispatcher;
            networkDispatcher.start();
        }

    }
复制代码

这里又建立了一个 CacheDispatcher 类。调用四个参数的构造方法。并调用了 start() 方法。 接下来,咱们就认识下 CacheDispatcher.java 类:

//原来它是一个线程
public class CacheDispatcher extends Thread {
//缓存队列,用BlockingQueue 管理存储
private final BlockingQueue<Request<?>> mCacheQueue;

   public CacheDispatcher(BlockingQueue<Request<?>> cacheQueue, BlockingQueue<Request<?>> networkQueue, Cache cache, ResponseDelivery delivery) {
       //参数赋值
        this.mCacheQueue = cacheQueue;
        this.mNetworkQueue = networkQueue;
        this.mCache = cache;
        this.mDelivery = delivery;
    }
//调用start 方法一定调用run 方法
 public void run() {
        .....
        Process.setThreadPriority(10);
      //这里初始化缓存,还记得咱们以前默认传入了一个 NoCache 吗?
    //这里Cache 是接口,子类有两种NoCache 和 DiskBasedCache两种
        this.mCache.initialize();
      //嵌套了好多循环啊......由于要不断去读取是否有任务嘛,没有的时候就一直等待
        while(true) {
            while(true) {
                while(true) {
                    while(true) {
                        try {
                          // 表示从缓存队列中取出一个 Request, 那第一次确定没有啊,就一直等待......
                            final Request<?> request = (Request)this.mCacheQueue.take();
                         .....这里我先省略了,由于还没真正到这一步
}
复制代码

OK,返回到咱们以前的操做: RequestQueue.java

private NetworkDispatcher[] mDispatchers ;
private static final int DEFAULT_NETWORK_THREAD_POOL_SIZE = 2;
 public RequestQueue(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery) {
         ....
        this.mDispatchers = new NetworkDispatcher[threadPoolSize];
        ....
    }

public void start() {
        this.stop();
        this.mCacheDispatcher = new CacheDispatcher(this.mCacheQueue, this.mNetworkQueue, this.mCache, this.mDelivery);
        this.mCacheDispatcher.start();

      //到这里啦,
      //从构造方法咱们能够得知 mDispatchers.length = 2 ,上
        for(int i = 0; i < this.mDispatchers.length; ++i) {
          
            NetworkDispatcher networkDispatcher = new NetworkDispatcher(this.mNetworkQueue, this.mNetwork, this.mCache, this.mDelivery);
            this.mDispatchers[i] = networkDispatcher;
            networkDispatcher.start();
        }

    }
复制代码

循环遍历生成2 个 NetworkDispatcher 对象,并将 NetworkDispatcher 对象存储在一个 mDispatchers 的数组中去了,最后调用了 start 方法。 Ok,那接下来咱们就看下这个 NetworkDispatcher.java 类了。

NetworkDispatcher.java

public class NetworkDispatcher extends Thread {
      // 网络请求队列
       private final BlockingQueue<Request<?>> mQueue;

        //对象初始化
        public NetworkDispatcher(BlockingQueue<Request<?>> queue, Network network, Cache cache, ResponseDelivery delivery) {
        this.mQueue = queue;
        this.mNetwork = network;
        this.mCache = cache;
        this.mDelivery = delivery;
    }   
//既然是线程,调用 start 方法,一定调用 run 方法
public void run() {
        Process.setThreadPriority(10);

      //线程也是,既然要作网络请求,就要一直等待获取
        while(true) {
            Request request;
            while(true) {
                try {
                  // 从网络请求队列中获取任务,那一开始咱们初始化确定没东西,队列里没请求任务
                    request = (Request)this.mQueue.take();
                    break;
                } catch (InterruptedException var4) {
                    if (this.mQuit) {
                        return;
                    }
                }
            }
      .....底部代码我也省略了,由于都是获取到请求以后所作的处理      
复制代码

至此,对于RequestQueue 的初始化第一步咱们完成了对它的了解,你明白了吗? 下一篇咱们针对 mQueue.add(request) 真正须要进行网络请求进行继续分析。若是这篇文章有帮助到你,给个赞就是我最大的鼓励了,比心💗。

相关文章
相关标签/搜索