Android进阶-Volley-3. Network&NetworkResponse
参考链接:
1. Network
Network是一个接口,包含唯一一个方法performRequest,该方法执行request,并返回一个NetworkResponse。Volley中默认使用BasicNetwork类implement该接口。在BasicNetwork的performRequest中,其通过httpResponse =mHttpStack.performRequest(request, headers);获取httpResponse,其中mHttpStack是一个HttpStack接口,其具体实现由Android版本号决定(Volley.java):
if (Build.VERSION.SDK_INT >= 9) { stack = new HurlStack(); } else { // Prior to Gingerbread, HttpUrlConnection was unreliable. // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent)); }
现在大多为HttpURLConnection,也就是code中的HurlStack,其利用HttpURLConnection中的API执行网络请求,并在执行完成后返回HttpResponse类。若HttpResponse类通信正常,则将其中的内容解析出来,封装为NetworkResponse类返回。
2. NetworkResponse
NetworkResponse的构造函数之一为:public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers, boolean notModified)
主要包含四个变量:int statusCode-HTTP状态码、byte[] data-response中的原始数据(Raw Data)、Map<String,String> headers-返回头、boolean notModified-服务器是否返回304。
304 的标准解释是:NotModified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档)。服务器告诉客户,原来缓冲的文档还可以继续使用。
3. Request-解析NetworkResponse
Request中有这么一个函数:
abstract protected Response<T>parseNetworkResponse(NetworkResponse response);
接下来以StringRequest为例,看看其是如何对NetworkResponse进行解析的:
protected Response<String> parseNetworkResponse(NetworkResponse response) { String parsed; try { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); } return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response)); }
回到NetworkDispatcher的run中,在其通过:
Response<?>response = request.parseNetworkResponse(networkResponse);
获得解析出的Response后,执行mDelivery.postResponse(request, response)。其中mDelivery的类型为ResponseDelivery,这是一个接口。查看NetworkDispatcher.java的代码可看到mDelivery在其构造函数中被初始化:
public NetworkDispatcher(BlockingQueue<Request> queue, Network network, Cache cache, ResponseDelivery delivery) { mQueue = queue; mNetwork = network; mCache = cache; mDelivery = delivery; }
NetworkDispatcher的start在RequestQueue.java的start中:
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(); } }
查看RequestQueue的构造函数可知mDelivery是:new ExecutorDelivery(new Handler(Looper.getMainLooper()
ExecuteDelivery中的postResponse函数执行了:mRequest.deliverResponse(mResponse.result);在StringRequest中,deliverResponse回调了onResponse函数:mListener.onResponse(response);
图示Volley(图源自codekk的volley源码解析:
总体设计图:
流程图:
类关系图:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。