Volley学习第三篇-网络线程

前文概要

其实NetworkDispatcherCacheDispatcher是很相像的.感觉看NetworkDispatcher要比CacheDispatcher要简单.

因为它不需要进行判断缓存的过期时间和新鲜时间,仅仅是执行请求,然后在缓存响应结果,所以要简单些.

流程分析

同样的继承Thread,只看run()方法里面的就好了

同样的死循环,队列无内容时阻塞

  • 1.首先从队列中取出Request请求

    request = mQueue.take();
  • 2.判断请求是否取消

    if (request.isCanceled()) {
        request.finish("network-discard-cancelled");
        continue;
    }
  • 3.请求未取消,执行网络请求并得到NetworkResponse对象

    NetworkResponse networkResponse = mNetwork.performRequest(request);

    具体的网络请求还是需要看Network接口的实现类中是如何操作的.

  • 4.判断服务器返回码是否是304(资源未更新)

    if (networkResponse.notModified && request.hasHadResponseDelivered()) {
        request.finish("not-modified");
        continue;
    }
  • 5.是否缓存请求的响应

    if (request.shouldCache() && response.cacheEntry != null) {
        mCache.put(request.getCacheKey(), response.cacheEntry);
        request.addMarker("network-cache-written");
    }
  • 6.分发

    mDelivery.postResponse(request, response);

小结

其实看明白先前的缓存线程之后,理解网络线程也不是难事了.

明天详细解释下具体响应的转化和分发流程.

Volley学习第二篇-缓存流程

前文概要

上篇说道Volley初始化的时候需要创建一个RequestQueue消息队列,下面就来看看这个RequestQueue.

RequestQueue

是一个队列管理器,里面维护了两个队列—CacheQueueNetowrkQueue.

Volley类里面的newRequestQueue方法中调用了队列的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();

    // 这里默认创建4个线程
    // 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();
    }
}

这里创建了1个CacheDispatcher和4个NetworkDispatcher.CacheDispatcher和4个NetworkDispatcher都是继承了Thread的两个线程,一个是缓存线程,另一个是网络线程.

其中DEFAULT_NETWORK_THREAD_POOL_SIZE中定义了网络线程的个数,可以根据不同的cpu核数来自定义开多少个网络线程(线程数 = cpu核数 * 2 + 1).

CacheDispatcher缓存线程的流程

它继承了Thread,所以只需要看它的run()方法就好了.

while (true) {
    try {
        // Get a request from the cache triage queue, blocking until
        // at least one is available.从缓存队列中不停的取出request,直到队列中只有一个请求的时候阻塞
        final Request request = mCacheQueue.take();
        request.addMarker("cache-queue-take");

        // If the request has been canceled, don't bother dispatching it.
        if (request.isCanceled()) {
            request.finish("cache-discard-canceled");
            continue;// 请求取消就不读缓存了
        }

        // Attempt to retrieve this item from cache. 从缓存中获取缓存信息的实体
        Cache.Entry entry = mCache.get(request.getCacheKey());
        if (entry == null) {
            request.addMarker("cache-miss");
            // Cache miss; send off to the network dispatcher.
            mNetworkQueue.put(request);// 缓存木有,将请求添加到网络请求队列
            continue;
        }

        // If it is completely expired 过期, just send it to the network.
        if (entry.isExpired()) {
            request.addMarker("cache-hit-expired");
            request.setCacheEntry(entry);
            mNetworkQueue.put(request);// 缓存过期,添加到网络请求队列
            continue;
        }

        // We have a cache hit; parse its data for delivery back to the request.
        request.addMarker("cache-hit");
        // 解析网络数据  这个是由请求对象request来解析的
        // 文档中说道:request对象负责请求和解析网络请求
        Response<?> response = request.parseNetworkResponse(
                new NetworkResponse(entry.data, entry.responseHeaders));
        request.addMarker("cache-hit-parsed");

        if (!entry.refreshNeeded()) {// 缓存是否需要刷新
            // Completely unexpired cache hit. Just deliver the response.
            mDelivery.postResponse(request, response);// 无需刷新,直接分发
        } else {
            // Soft-expired cache hit. We can deliver the cached response,
            // but we need to also send the request to the network for
            // refreshing.
            request.addMarker("cache-hit-refresh-needed");
            request.setCacheEntry(entry);// 更新缓存

            // Mark the response as intermediate.// 中间  媒介
            response.intermediate = true;

            // Post the intermediate response back to the user and have
            // the delivery then forward the request along to the network.
            mDelivery.postResponse(request, response, new Runnable() {
                @Override
                public void run() {
                    try {
                        mNetworkQueue.put(request);
                    } catch (InterruptedException e) {
                        // Not much we can do about this.
                    }
                }
            });
        }

    } catch (InterruptedException e) {
        // We may have been interrupted because it was time to quit.
        if (mQuit) {
            return;
        }
        continue;
    }
}

缓存的主体流程就在这个死循环里面,Volley的dispatcher的原理和Handler里面的looper的原理非常相似.

读取缓存分发到主线程

Response<?> response = request.parseNetworkResponse(new NetworkResponse(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed");

if (!entry.refreshNeeded()) {
    // Completely unexpired cache hit. Just deliver the response.
    mDelivery.postResponse(request, response);
} else {
    // Soft-expired cache hit. We can deliver the cached response,
    // but we need to also send the request to the network for
    // refreshing.
    request.addMarker("cache-hit-refresh-needed");
    request.setCacheEntry(entry);

    // Mark the response as intermediate.// 中间  媒介
    response.intermediate = true;

    // Post the intermediate response back to the user and have
    // the delivery then forward the request along to the network.
    mDelivery.postResponse(request, response, new Runnable() {
        @Override
        public void run() {
            try {
                mNetworkQueue.put(request);
            } catch (InterruptedException e) {
                // Not much we can do about this.
            }
        }
    });
}

详解

request.parseNetworkResponse(new NetworkResponse(entry.data, entry.responseHeaders));`

entry.data是缓存的原始的byte数组,将byte数组响应头封装成一个NetworkResponse对象(Volley里面的网络响应的统一对象).
parseNetworkResponse()方法将NetworkResponse对象解析成Response供各种泛型的转换.

mDelivery.postResponse(request, response);

mDelivery对象时在CacheDispatcher的构造方法的时候赋值的,往上找找RequestQueue中的start()方法中mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);

继续找

public RequestQueue(Cache cache, Network network, int threadPoolSize,ResponseDelivery delivery) {
    mCache = cache;
    mNetwork = network;
    mDispatchers = new NetworkDispatcher[threadPoolSize];
    mDelivery = delivery;
}

还找

public RequestQueue(Cache cache, Network network, int threadPoolSize) {
    this(cache, network, threadPoolSize,
            new ExecutorDelivery(new Handler(Looper.getMainLooper())));
}

看mDelivery就是new ExecutorDelivery(new Handler(Looper.getMainLooper()))

ExecutorDelivery中构造方法中有一个非常重要的一行

public ExecutorDelivery(final Handler handler) {
    // Make an Executor that just wraps the handler.
    mResponsePoster = new Executor() {
        @Override
        public void execute(Runnable command) {
            handler.post(command);
        }
    };
}

handlerrunnable对象post出去,而handler是通过Looper.getMainLooper创建的,这样就是通过我们用主线程创建的Handler将响应发送到主线程中了.

至此,Volley缓存的读取/分发就完成了.

总结


官方图解中的绿色的那部分—缓存线程的流程就结束了.

后面我会慢慢肥西网络线程相关的东西.

Volley学习第一篇-框架入口.md

前文概要

这是我的Volley学习第一篇的笔记.
看了一个星期的各类教程之后的总结,准备把Volley的整体流程都总结一遍,同时会对Volley进行一些扩展,最后会跟着大神的脚步模仿一个Volley出来.

水平有限,望多指教.

资源

创建全局的RequestQueue

使用Volley的时候会创建一个全局的RequestQueue,一般会在自定义的Application类里面创建.

RequestQueue requestQueue = Volley.newRequestQueue(appContext);

Volley框架的入口点

这里我们就发现原来Volley的入口是Volley类里面的一个静态方法newRequestQueue()

public static RequestQueue newRequestQueue(Context context) {
    return newRequestQueue(context, null);
}

Volley框架的真正的入口

这是一个参数的方法,会调用两个参数的newRequestQueue方法,传入一个默认的null.

public static RequestQueue newRequestQueue(Context context, HttpStack stack) {

    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

    String userAgent = "volley/0";
    try {
        String packageName = context.getPackageName();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
        userAgent = packageName + "/" + info.versionCode;
    } catch (NameNotFoundException e) {
    }

    if (stack == null) {
        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));
        }
    }

    Network network = new BasicNetwork(stack);

    RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
    queue.start();

    return queue;
}

这里才是真正的创建RequestQueue的地方,因为传入的第二个参数是null,所以stack== null是成立的,这是就会根据不同的系统版本创建不同的HttpStack子类的实例对象,然后将它包装成BasicNetwork对象.

同时第一行的时候就创建了Volley的缓存目录(当然你可以根据你的需求随意更改缓存目录的位置),并将缓存目录包装成一个DiskBasedCache对象.

至此,RequestQueue需要的缓存和网络请求对象就创建成功了,然后我们的RequestQueue请求队列就创建成功了.

通过start()方法就启动了我们的请求队列.

小结

两个newRequestQueue方法使用的这种方式其实非常巧妙,以前我看到这种方式就叫他重载调用(不知道叫的对不对,哈哈).

这是我的Volley系列的第一篇,好的开始,加油↖(^ω^)↗!