博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【深蓝】Volley 全程模拟
阅读量:7071 次
发布时间:2019-06-28

本文共 4089 字,大约阅读时间需要 13 分钟。

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个参数,分别为 网络请求低劣,执行的请求方法,缓存,当然还有回调。

 

public <T> Request<T> add(Request<T> request)

需要的参数为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); }
Request

 

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;    }
findDefaultTrafficStatsTag

  如果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
> stagedRequests = mWaitingRequests.get(cacheKey); if (stagedRequests == null) { stagedRequests = new LinkedList
>(); } 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; }

 

是否已经在请求队列中等等。

这个是应该缓存的,第一步先判断

 

转载于:https://www.cnblogs.com/a-blue/p/3672122.html

你可能感兴趣的文章
iOS8 CIGlassDistortion滤镜的使用
查看>>
windows运行打开服务命令的方法 :
查看>>
【C++】atoi与stoi
查看>>
Webservice soap wsdl区别之个人见解
查看>>
An Introduction to Garbage Collection(垃圾回收简介)
查看>>
提高tomcat的并发能力
查看>>
Delphi 中Format的字符串格式化使用说明(转)
查看>>
Drag & drop a button widget
查看>>
【转】 java中HashMap详解
查看>>
ODAC(V9.5.15) 学习笔记(一)总论
查看>>
Linux动态库搜索路径的技巧
查看>>
关于开源的RTP——jrtplib的使用
查看>>
非常特别的一个动态规划新手教程
查看>>
android FragmentPagerAdapter的“标准”配置
查看>>
OAF 中对文字实现html效果及对超级长文本实现默认换行,messageTextInput只读后内容自动换行,...
查看>>
sql行列转换
查看>>
Myeclipse 的hadoop环境搭建
查看>>
重置mysql的root用户密码
查看>>
Why are Eight Bits Enough for Deep Neural Networks?
查看>>
带删除小图标的EditText
查看>>