之前项目中用过Volley框架,看了一下源码,今天就写一个关于Volley的文章。
Volley的使用是先创建一个RequestQueue,然后把自己的request add到RequestQueue里面,代码如下
mQueue = Volley.newRequestQueue(getApplicationContext()); ...StringRequest stringRequest = new StringRequest(requestType, url, new Response.Listener<String>() { @Override public void onResponse(String response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { return params; } @Override protected Map<String, String> getParams() throws AuthFailureError { return map; } };
...mQueue.add(request); 就怎么简单。 那现在就从 mQueue.add(request); 开始解析,先看RequestQueue的属性private AtomicInteger mSequenceGenerator; private final Map<String, Queue<Request>> mWaitingRequests; private final Set<Request> mCurrentRequests; //这个是放从缓存取的线程任务的队列 private final PriorityBlockingQueue<Request> mCacheQueue; //这个是放从网络请求的线程任务的队列 private final PriorityBlockingQueue<Request> mNetworkQueue; //默认线程数,这个初始化NetworkDispatcher的时候用 private static final int DEFAULT_NETWORK_THREAD_POOL_SIZE = 4; private final Cache mCache; private final Network mNetwork; private final ResponseDelivery mDelivery; //从网络取的线程数组 private NetworkDispatcher[] mDispatchers;//从缓存取的线程,就一个 private CacheDispatcher mCacheDispatcher;
再看创建 mQueue 的函数,最后调用这个函数,stack为null。public static RequestQueue newRequestQueue(Context context, HttpStack stack) { //缓存路径,这里就不讲了 File cacheDir = new File(context.getCacheDir(), "volley"); String userAgent = "volley/0"; try { String network = context.getPackageName(); PackageInfo queue = context.getPackageManager().getPackageInfo(network, 0); userAgent = network + "/" + queue.versionCode; } catch (NameNotFoundException var6) { ; } if(stack == null) { if(VERSION.SDK_INT >= 9) { //如果是9以上就用HTTPUrlConnection下载(为什么自己百度) stack = new HurlStack(); } else { //如果是9一下就用HttpClient下载 stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent)); } } BasicNetwork network1 = new BasicNetwork((HttpStack)stack); RequestQueue queue1 = new RequestQueue(new DiskBasedCache(cacheDir), network1); queue1.start(); return queue1; } 再看 queue1.start() 里面的内容public void start() { this.stop(); //创建一个从缓存取的线程并start线程 this.mCacheDispatcher = new CacheDispatcher(this.mCacheQueue, this.mNetworkQueue, this.mCache, this.mDelivery); this.mCacheDispatcher.start(); //创建length个从网络取的线程并start所有线程,线程里面是死循环从队列里面去Request并处理Request 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(); } } 现在看add(Request request)函数public Request add(Request request) { request.setRequestQueue(this); Set var2 = this.mCurrentRequests; synchronized(this.mCurrentRequests) { this.mCurrentRequests.add(request); } request.setSequence(this.getSequenceNumber()); request.addMarker("add-to-queue"); if(!request.shouldCache()) { //这个接口不缓存,所以不可能在缓存中,直接放在从网络下载的队列里面 this.mNetworkQueue.add(request); return request; } else { Map var8 = this.mWaitingRequests; synchronized(this.mWaitingRequests) { String cacheKey = request.getCacheKey(); if(this.mWaitingRequests.containsKey(cacheKey)) { //这个是已经有了一个请求同一个url的request Object stagedRequests = (Queue)this.mWaitingRequests.get(cacheKey); if(stagedRequests == null) { stagedRequests = new LinkedList(); } ((Queue)stagedRequests).add(request); this.mWaitingRequests.put(cacheKey, stagedRequests); if(VolleyLog.DEBUG) { VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", new Object[]{cacheKey}); } } else { this.mWaitingRequests.put(cacheKey, (Object)null); //在这里把请求添加到队列面 this.mCacheQueue.add(request); } return request; } } } 要想详细知道里面的内容,请阅读源码,这里只是你阅读源码之前的热身,有什么不对的地方请指出来,一起学习一起进。