Android进阶-Volley-3. Network&NetworkResponse

参考链接:

http://www.codekk.com/open-source-project-analysis/detail/Android/grumoon/Volley%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90

 

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源码解析:

http://www.codekk.com/open-source-project-analysis/detail/Android/grumoon/Volley%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90)

总体设计图:

流程图:

类关系图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值