GET
首先 要有一个RequestQueue 队列,实例化它。
public RequestQueue(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery) { mCache = cache; mNetwork = network; mDispatchers = new NetworkDispatcher[threadPoolSize]; mDelivery = delivery; }
最终需要的参数,第一个缓存,第二个网络请求,第三个网络线程的个数,第四个响应交付。
/** * 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(); } }
先直接开启这个线程,先stop 把现在所有的网络调度全部停止掉。
然后实例化一个缓存调度程序,几个networkDispatcher调度程序,调度程序其实就是一个Thread,然后开启他们。
而缓存需要的参数第一个为mCacheQueue缓存队列,先把请求添加到缓存队列,执行请求,如果没有结果,会添加到第二个队列mNetworkQueue网络队列,等待网络调度,
mCache也就是从这里找缓存,mDelivery 就是相当于结果回调。
网络调度程序 也有4个参数,分别为 网络请求低劣,执行的请求方法,缓存,当然还有回调。
需要的参数为request
/** * Creates a new request with the given method (one of the values from * {@link Method}), URL, and error listener. Note that the normal response * listener is not provided here as delivery of responses is provided by * subclasses, who have a better idea of how to deliver an already-parsed * response. */ public Request(int method, String url, Response.ErrorListener listener) { mMethod = method; mUrl = url; mErrorListener = listener; setRetryPolicy(new DefaultRetryPolicy()); mDefaultTrafficStatsTag = findDefaultTrafficStatsTag(url); }
1:请求类型,GET POST之类的。Request.Method.GET XXX
2:请求的URL地址。
3:错误结果回调监听器。
4:失败的时候重试策略。
5:看了里面的构造为
/** * @return The hashcode of the URL's host component, or 0 if there is none. */ private static int findDefaultTrafficStatsTag(String url) { if (!TextUtils.isEmpty(url)) { Uri uri = Uri.parse(url); if (uri != null) { String host = uri.getHost(); if (host != null) { return host.hashCode(); } } } return 0; }
如果Url 解析成Uri 然后返回Uri的HashCode
下面是add(Request )过程了。
request.setRequestQueue(this); 把request 关联到 请求队列中。
synchronized (mCurrentRequests) { mCurrentRequests.add(request); }
mCurrentRequests就是一个HashSet 不能有重复的的请求。
request.setSequence(getSequenceNumber()); 获取一个请求序列号。自动增加的
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; }
是否已经在请求队列中等等。
这个是应该缓存的,第一步先判断