Volley NetworkDispatcher

来源:互联网 发布:淘宝国新西兰能用吗 编辑:程序博客网 时间:2024/05/21 22:23

这是一个继承了线程Thread的类,用来具体处理网络请求,将request从BlockingQueue中取出来处理。这个类重写了run()方法具体会在这个方法中处理网络请求,存入缓存,以及response的分发。


@Override    public void run() {        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);        Request request;        while (true) {            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;                }                // Tag the request (if API >= 14)                //这个是用来统计流量的不过没看见用                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {                    TrafficStats.setThreadStatsTag(request.getTrafficStatsTag());                }                // 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.                //在另一个从cache取数据的线程中会将过了softTtl的Entry delivery然后在想服务器发送验证,所以如果这里收到304那么就不用再delivery了,如果收不到就按正常程序再delivery一遍。这里这两个判断条件应该是同事满足的,不能只满足一个吧。                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.                //这里shouldCache的值是用户设置的,但是如果服务器在http协议中指定数据不能缓存会在parseNetworkResponse()函数将response解析成Cache.Entry时返回null                //那么cache逻辑在将Cache.Entry解析成CacheHeader是后就会返回null从而不能完成缓存。CacheHeader和Cache.Entry的内容差不多                //就是多了key                //这个cacheEntry是在parseNetworkResponse()中被解析出来的这个类代表要存进cache的内容,而response包含被解析好的数据比如String Json这里的而原始数据                //还在cacheEntry的data中。response有一个函数Response(VolleyError error)会把cacheEntry置为null不过我还没找到他在哪被调用过、                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) {                parseAndDeliverNetworkError(request, volleyError);            } catch (Exception e) {                VolleyLog.e(e, "Unhandled exception %s", e.toString());                mDelivery.postError(request, new VolleyError(e));            }        }    }
0 0
原创粉丝点击