通过前面的学习我们已经掌握了Volley的基本用法,没看过的建议大家先去阅读我的博文【安卓网络请求开源框架Volley源码解析系列】初识Volley及其基本用法。如StringRequest用来请求一段文本信息,JsonRequest(JsonObjectRequest、JsonArrayRequest)用于请求一条JSON格式的数据,ImageRequest则是用于请求网络上的图片资源。但是我们知道在网络上传输的轻量级数据还包括另外一种即XML。但是Volley中目前还没提供用于请求XML格式的类,那我们如果我们要请求的是一段XML格式数据该怎们办?
答案很简单,自己动手写,还记得在初识Volley的博客中讲到的Volley的基本特点之一吗:可扩展性强。Volley提供了非常强的扩展机制,使得我们可以很轻松地定制出任意类型的Request。
一定制自己的Request请求
定制XMLRequest:去扩展不是自己开发的框架中的某些功能不是一件很容易的事,因为你很可能不知道从哪个地方作为切入点,因此我们首先要做的就是把谷歌官方写的一些与自己要定制的类功能相似的类好好研究一下,看能否看懂然后自己模仿。那么自然首先想到去研究一下StringRequest类的源码,代码如下:
/** * A canned request for retrieving the response body at a given URL as a String. */ public class StringRequest extends Request<String> { private final Listener<String> mListener; /** * Creates a new request with the given method. * * @param method the request {@link Method} to use * @param url URL to fetch the string at * @param listener Listener to receive the String response * @param errorListener Error listener, or null to ignore errors */ public StringRequest(int method, String url, Listener<String> listener, ErrorListener errorListener) { super(method, url, errorListener); mListener = listener; } /** * Creates a new GET request. * * @param url URL to fetch the string at * @param listener Listener to receive the String response * @param errorListener Error listener, or null to ignore errors */ public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) { this(Method.GET, url, listener, errorListener); } @Override protected void deliverResponse(String response) { mListener.onResponse(response); } @Override 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)); } }可以看到即使加上注释代码也没多少,我们首先从类的定义开始分析:
public class StringRequest extends Request<String>可以看到StringRequest继承自Request,而且提供了一个泛型参数String用来表示该类主要用来处理String类型的数据。这也是为何StringRequest中的代码如此简单的原因,因为复杂的网络请求的绝大部分公共功能都在抽象类Request类中实现了,我们只需要继承自它然后重写一些属于自己的特定的方法即可。因此我们知道如果我们要定制一个XMLRequest通过样也需要继承自Request,然后指定泛型参数为XML解析器即可。
接下来是其构造器,这个在初识Volley的博客中已经讲过,因此不再赘述。
然后是两个重写方法,这也是我们关注的重点:
@Override protected void deliverResponse(String response) { mListener.onResponse(response); } @Override 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)); }
abstract protected void deliverResponse(T response);可以看到参数为传入的泛型参数,该函数的功能即为当从服务器端获取数据成功时会自动回调该方法。采用的是观察者模式,即先定义一个泛型的Listener对象,然后再该方法中调用该对象的onResponse(response)方法。
第二个为 protected Response<String> parseNetworkResponse(NetworkResponse response),它同样是在其父类Request中定义的一个抽象方法,即所有继承自Request的子类必须重写它,其定义如下:
abstract protected Response<T> parseNetworkResponse(NetworkResponse response);可以看到参数为传入的泛型参数,顾名思义该函数的功能即为将从服务器端获取到的数据进行解析,是整个类中最核心的部分,也是不同功能的Request必须定制重写的一个方法,服务器端返回数据用NetworkResponse这个类来表示,其定义如下:
public class NetworkResponse { /** * Creates a new network response. * @param statusCode the HTTP status code * @param data Response body * @param headers Headers returned with this response, or null for none * @param notModified True if the server returned a 304 and the data was already in cache */ public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers, boolean notModified) { this.statusCode = statusCode; this.data = data; this.headers = headers; this.notModified = notModified; } public NetworkResponse(byte[] data) { this(HttpStatus.SC_OK, data, Collections.<String, String>emptyMap(), false); } public NetworkResponse(byte[] data, Map<String, String> headers) { this(HttpStatus.SC_OK, data, headers, false); } /** The HTTP status code. */ public final int statusCode; /** Raw data from this response. */ public final byte[] data; /** Response headers. */ public final Map<String, String> headers; /** True if the server returned a 304 (Not Modified). */ public final boolean notModified; }
/** Returns a successful response containing the parsed result. */ public static <T> Response<T> success(T result, Cache.Entry cacheEntry) { return new Response<T>(result, cacheEntry); }
讲到这我们基本上就从源码的角度彻底理解了StringRequest类,那么接下来我们就按照上述我为大家分析的步骤模仿写一个XMLRequest,代码如下:
public class XMLRequest extends Request<XmlPullParser> {//继承自Request类,指定泛型参数为XML解析器 private final Listener<XmlPullParser> mListener;//指定监听器的泛型参数为XML解析器 public XMLRequest(int method, String url, Listener<XmlPullParser> listener, ErrorListener errorListener) { super(method, url, errorListener); mListener = listener; } public XMLRequest(String url, Listener<XmlPullParser> listener, ErrorListener errorListener) { this(Method.GET, url, listener, errorListener); } @Override protected Response<XmlPullParser> parseNetworkResponse(NetworkResponse response) { try { String xmlString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); xmlPullParser.setInput(new StringReader(xmlString)); return Response.success(xmlPullParser, HttpHeaderParser.parseCacheHeaders(response));//返回包含该xml解析器的Response对象 } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (XmlPullParserException e) { return Response.error(new ParseError(e)); } } @Override protected void deliverResponse(XmlPullParser response) { mListener.onResponse(response); } }如果你了解XML解析的话那么上述代码对你而言将会相当的简单。下面我们就来使用这个我们自定义的XMLRequest。
XMLRequest xmlRequest = new XMLRequest( "http://flash.weather.com.cn/wmaps/xml/china.xml", new Response.Listener<XmlPullParser>() { @Override public void onResponse(XmlPullParser response) { try { int eventType = response.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: String nodeName = response.getName(); if ("city".equals(nodeName)) { String pName = response.getAttributeValue(0); Log.d("TAG", "pName is " + quName); } break; } eventType = response.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); } }); mQueue.add(xmlRequest);
讲到这基本上就把Volley的用法及其扩展全部讲完了,相信大家对Volley使用已经相当熟练了,但这还不够,接下来我们不再是从单个类的源码角度来解析Volley,而是从整个框架体系角度来解析Volley,让你彻底了解Volley的整个框架体系。
二Volley框架源码剖析
首先我们来看一下谷歌官方文档中Volley的框架图:
从这个图可以看到整个框架体系包括至少三个线程:主线程(main thread),缓存调度线程(cache thread),网络调度线程(network thread)。那么我们首先从主线程入手。
可以看到在主线程中会根据优先级将请求添加到缓存队列。所谓缓存队列就是前面我们每次使用Volley时都要创建的RequestQueue对象。那么我们首先来看一下其源代码:
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; } /** * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it. * * @param context A {@link Context} to use for creating the cache dir. * @return A started {@link RequestQueue} instance. */ public static RequestQueue newRequestQueue(Context context) { return newRequestQueue(context, null); }其中第二个即为我们每次使用Volley时调用的即Volley.newRequestQueue(context)。可以看到在该方法中事实上是调用的带两个参数的重载方法,即第一个方法。可以看到首先指定了硬盘缓存的位置为data/data/package_name/cache/volley/...,然后判断SDK的版本号,如果大于等于9则会创建一个HurlStack(),否则创建一个HttpClientStack(),而实际上HurlStack的内部就是使用HttpURLConnection进行网络通讯的,而HttpClientStack的内部则是使用HttpClient进行网络通讯的,至于为何这么选择在我的博文 【安卓网络请求开源框架Volley源码解析系列】初识Volley及其基本用法已介绍过,在此不再赘述。创建好了HttpStack之后,接下来又创建了一个Network对象(具体实现类是BasicNetwork),它是用于根据传入的HttpStack对象来处理网络请求的,然后创建一个请求队列RequestQueue调用其start()启动,最后返回该请求队列。
那么最核心的当然是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(); } }可以看到,该方法代码逻辑非常简单,创建了一个CacheDispatcher和4个NetworkDispatcher个对象,然后调用start(),这里的CacheDispatcher和NetworkDispatcher都是继承自Thread的,其中CacheDispatcher是缓存线程,NetworkDispatcher是网络请求线程。可以看到在CacheDispatcher的构造器中传入了缓存请求队列(mCacheQueue),网络请求队列(mNetworkQueue),硬盘缓存对象(DiskBasedCache),结果分发器(mDelivery)。之所以也传入网络请求队列是因为一部分缓存请求可能已经过期了,这时候需要重新从网络获取。NetworkDispatcher除了缓存请求队列没有传入,其他跟CacheDispatcher一样。到这里RequestQueue的任务就完成了,以后有请求都会交由这些dispatcher线程处理。
创建了请求队列之后我们只需要构建出相应的Request,然后调用RequestQueue的add()方法将Request传入就可以完成网络请求操作,那么接下来我们来看一下请求添加
即add()的源码:
/** * Adds a Request to the dispatch queue. * @param request The request to service * @return The passed-in request */ public Request add(Request request) { // Tag the request as belonging to this queue and add it to the set of current requests. request.setRequestQueue(this); synchronized (mCurrentRequests) { mCurrentRequests.add(request); } // Process requests in the order they are added. request.setSequence(getSequenceNumber()); request.addMarker("add-to-queue"); // If the request is uncacheable, skip the cache queue and go straight to the network. if (!request.shouldCache()) { mNetworkQueue.add(request); return request; } // Insert request into stage if there's already a request with the same cache key in flight. synchronized (mWaitingRequests) { String cacheKey = request.getCacheKey(); if (mWaitingRequests.containsKey(cacheKey)) { // There is already a request in flight. Queue up. Queue<Request> stagedRequests = mWaitingRequests.get(cacheKey); if (stagedRequests == null) { stagedRequests = new LinkedList<Request>(); } stagedRequests.add(request); mWaitingRequests.put(cacheKey, stagedRequests); if (VolleyLog.DEBUG) { VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey); } } else { // Insert 'null' queue for this cacheKey, indicating there is now a request in // flight. mWaitingRequests.put(cacheKey, null); mCacheQueue.add(request); } return request; } }可以看到add的大致逻辑如下:
1.将请求加入mCurrentRequests集合(注意此行为是同步的)
2.为请求添加序列号
3.判断是否应该缓存请求,如果不需要,加入网络请求队列
4.如果有相同请求正在被处理,加入到相同请求等待队列中,否则加入缓存请求队列。
通过上述分析我们可以知道add方法仅仅只起到添加请求的作用,而请求的处理是CacheDispatcher和NetworkDispatcher来完成的。这也正好与上述给的Volley的框架流程图相符。我们来看一下其源码:
public class CacheDispatcher extends Thread { private static final boolean DEBUG = VolleyLog.DEBUG; /** The queue of requests coming in for triage. */ private final BlockingQueue<Request> mCacheQueue; /** The queue of requests going out to the network. */ private final BlockingQueue<Request> mNetworkQueue; /** The cache to read from. */ private final Cache mCache; /** For posting responses. */ private final ResponseDelivery mDelivery; /** Used for telling us to die. */ private volatile boolean mQuit = false; /** * Creates a new cache triage dispatcher thread. You must call {@link #start()} * in order to begin processing. * * @param cacheQueue Queue of incoming requests for triage * @param networkQueue Queue to post requests that require network to * @param cache Cache interface to use for resolution * @param delivery Delivery interface to use for posting responses */ public CacheDispatcher( BlockingQueue<Request> cacheQueue, BlockingQueue<Request> networkQueue, Cache cache, ResponseDelivery delivery) { mCacheQueue = cacheQueue; mNetworkQueue = networkQueue; mCache = cache; mDelivery = delivery; } /** * Forces this dispatcher to quit immediately. If any requests are still in * the queue, they are not guaranteed to be processed. */ public void quit() { mQuit = true; interrupt(); } @Override public void run() { if (DEBUG) VolleyLog.v("start new dispatcher"); Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); // Make a blocking call to initialize the cache. mCache.initialize(); while (true) { try { // Get a request from the cache triage queue, blocking until // at least one is available. 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"); 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; } } } }重点关注run()方法,因为CacheDispatcher继承自Thread,其重要的逻辑处理都是在run()方法中完成的。可以看到在run()方法中存在一个while(true)死循环,即
缓存线程是一直在运行的,在该while循环中的处理过程大致如下:
1首先从缓存中取出一个请求,如果结果为空,则把该请求加入到网络请求中,否则对缓存结果进行过期判断。
2如果过期了,则加入网络请求队列。如果没有过期,那么通过request.parseNetworkResponse方法将硬盘缓存中的数据构造为Response对象(Request的parseNetworkResponse是抽象的,需要子类重写)。
在来看看网络请求线程的源码:
public class NetworkDispatcher extends Thread { /** The queue of requests to service. */ private final BlockingQueue<Request> mQueue; /** The network interface for processing requests. */ private final Network mNetwork; /** The cache to write to. */ private final Cache mCache; /** For posting responses and errors. */ private final ResponseDelivery mDelivery; /** Used for telling us to die. */ private volatile boolean mQuit = false; /** * Creates a new network dispatcher thread. You must call {@link #start()} * in order to begin processing. * * @param queue Queue of incoming requests for triage * @param network Network interface to use for performing requests * @param cache Cache interface to use for writing responses to cache * @param delivery Delivery interface to use for posting responses */ public NetworkDispatcher(BlockingQueue<Request> queue, Network network, Cache cache, ResponseDelivery delivery) { mQueue = queue; mNetwork = network; mCache = cache; mDelivery = delivery; } /** * Forces this dispatcher to quit immediately. If any requests are still in * the queue, they are not guaranteed to be processed. */ public void quit() { mQuit = true; interrupt(); } @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. 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) { parseAndDeliverNetworkError(request, volleyError); } catch (Exception e) { VolleyLog.e(e, "Unhandled exception %s", e.toString()); mDelivery.postError(request, new VolleyError(e)); } } } private void parseAndDeliverNetworkError(Request<?> request, VolleyError error) { error = request.parseNetworkError(error); mDelivery.postError(request, error); } }同样我们也只需要关注run()方法中的代码,可以看到在该方法中同样存在一个while(true)死循环,即网络请求线程也是一直在运行的,在该while循环中的处理过程大致和缓存线程相同,也是构造Response对象,然后交由ResponseDelivery处理,但是这里的Response对象是通过NetworkResponse转化的,而这个NetworkResponse是从网络获取的,这里最核心的一行代码就是:
// Perform the network request. NetworkResponse networkResponse = mNetwork.performRequest(request);
4请求结果的分发与处理:
在NetworkDispatcher中收到了NetworkResponse这个返回值后又会调用Request的parseNetworkResponse()方法来解析NetworkResponse中的数据,以及将数据写入到缓存,这个方法的实现是交给Request的子类来完成的,因为不同种类的Request解析的方式也肯定不同。还记得我们上述自定义Request的方式吗?其中parseNetworkResponse()这个方法就是必须要重写的。
在解析完了NetworkResponse中的数据之后,又会调用ExecutorDelivery的postResponse()方法来回调解析出的数据,代码如下
@Override public void postResponse(Request<?> request, Response<?> response) { postResponse(request, response, null); } @Override public void postResponse(Request<?> request, Response<?> response, Runnable runnable) { request.markDelivered(); request.addMarker("post-response"); mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable)); }可以看到,在mResponsePoster的execute()方法中传入了一个ResponseDeliveryRunnable对象,这样做是为了保证该对象中的run()方法是在主线程当中运行的了,我们来看一下其源码:
private class ResponseDeliveryRunnable implements Runnable { private final Request mRequest; private final Response mResponse; private final Runnable mRunnable; public ResponseDeliveryRunnable(Request request, Response response, Runnable runnable) { mRequest = request; mResponse = response; mRunnable = runnable; } @SuppressWarnings("unchecked") @Override public void run() { // If this request has canceled, finish it and don't deliver. if (mRequest.isCanceled()) { mRequest.finish("canceled-at-delivery"); return; } // Deliver a normal response or error, depending. if (mResponse.isSuccess()) { mRequest.deliverResponse(mResponse.result); } else { mRequest.deliverError(mResponse.error); } // If this is an intermediate response, add a marker, otherwise we're done // and the request can be finished. if (mResponse.intermediate) { mRequest.addMarker("intermediate-response"); } else { mRequest.finish("done"); } // If we have been provided a post-delivery runnable, run it. if (mRunnable != null) { mRunnable.run(); } } } }同样重点关注run方法中的代码,我们可以看到如果请求成功会调用即
if (mResponse.isSuccess()) { mRequest.deliverResponse(mResponse.result);}而deliverResponse(mResponse.result)这个方法就是我们在自定义Request时需要重写的另外一个方法,每一条网络请求的响应都会回调到这个方法中,所以我们需要重写该方法,在这个方法中将响应的数据回调到Response.Listener的onResponse()方法中就可以了。
至此Volley的整个框架体系就解析完了,我们来总结一下:
1首先在主线程中创建一个请求队列:Volley.newRequestQueue(context)然后通过add()添加一条网络请求。
2这条请求会被先添加到缓存队列,如果在缓存队列中找到该请求则读取缓存解析,然后将解析结果返回给主线程
3如果没找到该请求则将该请求加入网络请求队列,发送HTTP请求,获取服务器端返回的结果,解析之后写入缓存,然后将解析结果返回主线程。
如果你再仔细看一下上述Volley框架体系结构图,你会看到上述三个步骤正好完全对应谷歌官方文档的架构体系流程图。