volley流程android,8、volley 源码解析之网络线程工作流程

文章摘要

1、volley 网络线程工作原理

2、volley 实现 分解原理

简介:

volley有两个主要的民工,CacheDispatcher以及NetworkDispatcher,也是两个线程,管理并处理Request任务。

volley为了保证大批量的网络请求以及数据解析不会影响到主线程的用户交互,使用了很多线程以及线程封装技巧。包括这里的Cache。

在用户发起网络请求后,volley就将用户的请求,丢到了本文介绍的缓存进程,缓存线程如果没有能力处理,就丢给网络线程,并告诉它,老大需要数据结果,你赶紧去网络上去拿。老大只要结果,不要过程。

主线程很忙,ResponseDelivery负责传递消息,伴君如伴虎,为了防止打扰到主线程的工作,ResponseDelivery也可以有一个线程,在目前的源码里,ResponseDelivery充分利用Handler的MessageQueue优势,管理并小心的将结果传递给主线程。

那么,本文就来介绍下网络线程的工作原理:

1、NetworkDispatcher 网络线程

1、Volley中有三个线程,NetworkDispatcher是其中的网络线程。

2、NetworkDispatcher网络线程的目的是同步网络数据、保存网络数据。

网络线程,只负责同步网络数据,不负责解析。解析工作交给请求来做,因为不同的请求,解析的方法、流程多不同,将这些工作交给发起方也就是Request来处理。

线程循环运行,线程原料来自mNetworkQueue,其中的来源主要包括两部分:

a)、主线程可通过RequestQueue.add方法将请求加入mNetworkQueue。

b)、mCacheQueue发现缓存数据已过期、或者需要再次从网络上来同步。

3、可以将工作流程简单归纳为以下几步:

a)、线程循环运行,线程原料来自mNetworkQueue。

b)、同步网络数据,并将网络返回的原始数据,封装成NetworkResponse。

c)、调用请求发起方(Request)的解析函数,得到result。

附:流程图

71a816a76114

2、实现分析

1、线程循环运行,获得Request对象

while (true) {

try {

// Get a request from the cache triage queue, blocking until

// at least one is available.

//1、hlwang:CacheDispatcher原料来自mCacheQueue,第一步,获得Request

final Request> request = mCacheQueue.take();

request.addMarker("cache-queue-take");

... ...

}

}

2、如果Request被用户取消了,则不再需要继续执行了

// If the request has been canceled, don't bother dispatching it.

//2、hlwang:如果request已取消,已经不需要继续了

if (request.isCanceled()) {

request.finish("cache-discard-canceled");

continue;

}

3、同步网络,得到原始数据,保存在NetworkResponse。

// Perform the network request.

//3、网络处理requests,并返回NetworkResponse

NetworkResponse networkResponse = mNetwork.performRequest(request);

request.addMarker("network-http-complete");

4、不需要将响应回调给主线程的情况。

// If the server returned 304 AND we delivered a response already,

// we're done -- don't deliver a second identical response.

//4、如果服务器返回状态码 = 304,同时该请求Request已经传递过,则不需要再次响应传递。

if (networkResponse.notModified && request.hasHadResponseDelivered()) {

request.finish("not-modified");

continue;

}

5、如何解析原始数据,只有请求方才知道,故:解析工作交给请求方

// Parse the response here on the worker thread.

//5、解析请求响应数据,得到Response数据

Response> response = request.parseNetworkResponse(networkResponse);

request.addMarker("network-parse-complete");

6、缓存操作,如果数据需要缓存,那么调用传递的缓存接口来缓存。

// Write to cache if applicable.

// TODO: Only update cache metadata instead of entire record for 304s.

//6、是否需要缓存并含有缓存数据,如果是,将cache数据加入到mCache

if (request.shouldCache() && response.cacheEntry != null) {

mCache.put(request.getCacheKey(), response.cacheEntry);

request.addMarker("network-cache-written");

}

7、通过mDelivery,将数据返回给主线程。

// Post the response back.

//7、将reponse 返回

request.markDelivered();

mDelivery.postResponse(request, response);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Volley是一个优秀的安卓开源网络访问工具,这里包含了volley jar包和源码,版本是2015.07.28的1.0.18版本。更多资料可以参见volley的github地址: https://github.com/mcxiaoke/android-volley 1.0.18 2015.07.28 merge upstream, process response bodies only when present tweak getCacheKey(), using method and original url, fix #67 #78 add RedirectError, wrong exception for http 301 and 302, fix #51 make NetworkResponse Serializable, fix #53 create NetworkError using IOException, not using null response 1.0.17 2015.07.13 merge upstream, stop logging slow requests outside of debug logs merge pr #64, fix leaking the last request object 1.0.16 2015.05.18 fix duplicate retry, change DEFAULT_MAX_RETRIES to 0 merge pr, fix NegativeArraySizeException merge upstream, Use a BufferedOutputStream to read and write cache 1.0.15 2015.03.18 add two missing constructors for JsonArrayRequest and JsonObjectRequest add unique identifier for Request 1.0.14 2015.03.17 add more constructors for JsonArrayRequest and JsonObjectRequest update and fix gradle build, using new build.gradle 1.0.13 2015.03.16 merge pr, added constructor to JSONArrayRequest 1.0.12 2015.03.12 merge upstream, fix ImageLoader.getCacheKey() merge upstream, Fix broken DiskBasedCache merge upstream, Modify header parser to handle must-revalidate. 1.0.11 2015.03.03 merge upstream, Add a RequestFinishedListener to RequestQueue. merge upstream, Change the default character encoding for JSON responses to UTF-8 1.0.10 2014.12.30 merge upstream, Use the view size and scale type to restrict NIV requests. merge pr, Add a getImageURL method to NetworkImageView merge pr, Add the options of setting DiskBasedCache sizes in Volley.java 1.0.9 2014.11.26 merge upstream, Fix deprecation warnings (now errors in 0.14.4) w/ gradle. 1.0.8 2014.11.07 merge upstream, Metric for network time and getBackoffMultiplier() 1.0.7 2014.10.13 merge upstream, Add locale to HttpHeaderParserTest.rfc1123Date(long millis) merge upstream, Copy cache header for 304 response 1.0.6 2014.09.10 merge pr, fixed bug where Disk cache misses for redirects merge upstream, Fix crash/OOM in DiskBasedCache merge upstream, allow use of custom ImageRequests in ImageLoader 1.0.5 2014.06.18 merge upstream, Fixes bug in PATCH method. Sets the method before setting the body. update gradle and build tools version, fix build 1.0.4 2014.05.04 merge upstream, Guard against NullPointerException currently occurring in Volley when a Request is given a url whose host is null. 1.0.3 2014.04.01 merge upstream, ImageLoader: setError after null check 1.0.2 2014.02.28 merge upstream, Use the view size to restrict NIV requests. merge upstream, Fix generic type parameter for RequestQueue.add(Request) merge pr, added support for handling 301 and 302 http redirects using standalone deploy gradle build file and script 1.0.1 2014.02.13 add gradle build support, add readme, release using gradle 1.0.0 2014.01.03 create volley mirror at github, release first version

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值