美文网首页
Volley源码学习3-NetworkDispatcher类

Volley源码学习3-NetworkDispatcher类

作者: 依然淳熙 | 来源:发表于2017-10-10 18:20 被阅读0次

    先看看google给的注释:

    /**
     * Provides a thread for performing network dispatch from a queue of requests.
     *
     * Requests added to the specified queue are processed from the network via a
     * specified {@link Network} interface. Responses are committed to cache, if
     * eligible, using a specified {@link Cache} interface. Valid responses and
     * errors are posted back to the caller via a {@link ResponseDelivery}.
     */
    

    在RequestQueue 中构造了此类的对象并且调用了其start方法,下来我们来看看它是怎么执行的。因为继承了Thread,来看一看其run方法 。

     @Override
        public void run() {
            //将线程设置为最高级
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            while (true) { //如果调用RequestQueue的add方法,这里是while(true)的,将会在这马上收到请求。
                //获取系统时间
                long startTimeMs = SystemClock.elapsedRealtime();
                Request<?> request;
                try {
                    // Take a request from the queue.从队列中取出请求
                    request = mQueue.take();
                } catch (InterruptedException e) {
                    // We may have been interrupted because it was time to quit.
                    if (mQuit) {
                        return;
                    }
                    continue;
                }
    
                try {
                    //添加状态标记
                    request.addMarker("network-queue-take");
    
                    // If the request was cancelled already, do not perform the
                    // network request.
                    //如果放弃请求,则销毁请求
                    if (request.isCanceled()) {
                        request.finish("network-discard-cancelled");
                        request.notifyListenerResponseNotUsable();
                        continue;
                    }
                    //????
                    addTrafficStatsTag(request);
    
                    // Perform the network request.//请求网络 用mNetwork请求网络得到NetworkResponse
                    NetworkResponse networkResponse = mNetwork.performRequest(request);
                    //添加状态标记
                    request.addMarker("network-http-complete");
    
                    // If the server returned 304 AND we delivered a response already,
                    // we're done -- don't deliver a second identical response. 
                    //304的状态 以及处理
                    if (networkResponse.notModified && request.hasHadResponseDelivered()) {
                        request.finish("not-modified");
                        request.notifyListenerResponseNotUsable();
                        continue;
                    }
    
                    // Parse the response here on the worker thread. 在工作线程对响应进行parse
                    Response<?> response = request.parseNetworkResponse(networkResponse);
                   //添加状态标记
                    request.addMarker("network-parse-complete");
    
                    // Write to cache if applicable.
                    // TODO: Only update cache metadata instead of entire record for 304s.
                    if (request.shouldCache() && response.cacheEntry != null) {
                        //将响应结果存入缓存
                        mCache.put(request.getCacheKey(), response.cacheEntry);
                        request.addMarker("network-cache-written");
                    }        
                   // Post the response back.
                    request.markDelivered();
                    //通过mDelivery 将request和response 发送回主线程
                    mDelivery.postResponse(request, response);
                    request.notifyListenerResponseReceived(response);
                } catch (VolleyError volleyError) {
                    //错误处理
                    volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
                    parseAndDeliverNetworkError(request, volleyError);
                    request.notifyListenerResponseNotUsable();
                } catch (Exception e) {
                    //错误处理
                    VolleyLog.e(e, "Unhandled exception %s", e.toString());
                    VolleyError volleyError = new VolleyError(e);
                    volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
                    mDelivery.postError(request, volleyError);
                    request.notifyListenerResponseNotUsable();
                }
            }
        }
    
    
        private void parseAndDeliverNetworkError(Request<?> request, VolleyError error) {
            error = request.parseNetworkError(error);
            mDelivery.postError(request, error);
        }
    

    这就是NetworkDispatcher 分发的过程,接下来将分析BasicNetwork 类。

    相关文章

      网友评论

          本文标题:Volley源码学习3-NetworkDispatcher类

          本文链接:https://www.haomeiwen.com/subject/twlbyxtx.html