zoukankan      html  css  js  c++  java
  • Volley源码分析(三)NetWorkDispatcher分析

    NetWorkDispatcher分析

    NetWorkDispatcher和CacheDispatcher一样,继承于Thread,在run方法中实现一个无限循环,代码如下

     @Override
        public void run() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            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");
                        continue;
                    }
    
                    addTrafficStatsTag(request);
    
                    // Perform the network request.
                    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.
                    if (networkResponse.notModified && request.hasHadResponseDelivered()) {
                        request.finish("not-modified");
                        continue;
                    }
    
                    // Parse the response here on the worker thread.
                    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.postResponse(request, response);
                } catch (VolleyError volleyError) {
                    volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
                    parseAndDeliverNetworkError(request, volleyError);
                } 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,然后判断该request是否取消了,如果取消就结束,然后调用network的performRequest方法进行请求得到netWorkResponse,判断该response是否是304,如果是的话,或者已经发送了相同的response,就不在发送了。因为304是只返回了header无body,因为该request的结果已经缓存了。然后就进行networkresponse的解析,然后判断该request是否需要缓存,如果需要就放入cache中,然后mDelivery.postResponse(request, response);

    image

  • 相关阅读:
    Ubuntu下将python从2.7升级到3.5
    Python:IOError: image file is truncated 的解决办法
    Google Hack
    Python:将utf-8格式的文件转换成gbk格式的文件
    Python:字符编码详解
    IIS下使用appcmd批量搭建网站
    C#:注册机的实现【提供源代码下载】
    C#:实现快捷键自定义设置
    C#:如何解决WebBrowser.DocumentCompleted事件的多次调用
    C#:WebBrowser中伪造referer,为何对流量统计器无效?
  • 原文地址:https://www.cnblogs.com/qifengshi/p/7069685.html
Copyright © 2011-2022 走看看