Android中关于Volley的使用(三)认识Volley的架构

来源:互联网 发布:网络词语大全及解释 编辑:程序博客网 时间:2024/05/14 19:06
前面我们讲了怎么应用Volley从网络获取图片跟JSON数据,具体的应用如下:
1)通过Volley类获得一个RequestQueue对象:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. mQueue = Volley.newRequestQueue(this);    

2)创建一个Request对象,并将请求添加上面创建的mQueue中,如下:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ImageRequest imgRequest = new ImageRequest(imgUrl,    
  2.         new Response.Listener<Bitmap>() {    
  3.             @Override    
  4.             public void onResponse(Bitmap arg0) {    
  5.                 // TODO Auto-generated method stub    
  6.                 imageView.setImageBitmap(arg0);    
  7.             }    
  8.         },     
  9.         300,     
  10.         200,     
  11.         Config.ARGB_8888,     
  12.         new ErrorListener() {    
  13.             @Override    
  14.             public void onErrorResponse(VolleyError arg0) {    
  15.                     
  16.             }    
  17.         });    
  18. mQueue.add(imgRequest);  

一开始会不会觉得有点怔,为什么只需要将请求添加到队列中,不需要去发送请求吗?当然是要的,只是这一切都被封装在Volley后面而已。
我们先来看一下隐藏在Volley后面的这个架构是怎么样的吧,如下图:
首先,我们要先了解一下Volley的架构,简单地来说,就是三层:
1)队列:将请求添加到Queue中
2)缓存:在Cache中查找有没有缓存这个请求要的数据,
2.1)有,请求如果过期了,到第 3)步,如果没有过期,直接返回一个Response。
2.2)没有,到第 3)步
3)网络:通过传进来的url 来发送请求,到网络中获取数据,然后返回一个Response,并判断是否要存到Cache中。
结合上图,我们来简单地先说一下Volley的流程是怎么样的。
第一步:通过Volley.newRequestQueue方法创建一个RequestQueue队列,而且同时会创建如下三个对象:
a)一个HttpStack对象,Volley会根据SDK的版本来判断是基于HttpUrlConnection还是基于HttpClient来创建对象,一个HttpStack对象,只用一件事,就是去真正的执行这个请求,去网络上拿回数据,其定义如下:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public interface HttpStack {  
  2.     /** 
  3.      * Performs an HTTP request with the given parameters. 
  4.      * 
  5.      * <p>A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise, 
  6.      * and the Content-Type header is set to request.getPostBodyContentType().</p> 
  7.      * 
  8.      * @param request the request to perform 
  9.      * @param additionalHeaders additional headers to be sent together with 
  10.      *         {@link Request#getHeaders()} 
  11.      * @return the HTTP response 
  12.      */  
  13.     public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)  
  14.         throws IOException, AuthFailureError;  
  15. }  


b)一个Network对象,在Volley的实现是BasicNetwork类,在构建这个对象的时候,一个HttpStack对象去传给它作为参数,而它将会调用这个HttpStack去获取事情。它是对HttpUrlConnection和HttpClient的一个包装,让外面的对象不用去关心到底是通过哪个接口来获取数据。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public interface Network {  
  2.     /** 
  3.      * Performs the specified request. 
  4.      * @param request Request to process 
  5.      * @return A {@link NetworkResponse} with data and caching metadata; will never be null 
  6.      * @throws VolleyError on errors 
  7.      */  
  8.     public NetworkResponse performRequest(Request<?> request) throws VolleyError;  
  9. }  

看一下它的实现类BasicNetwork中是如何实现的这个接口中的方法的,看下面代码中的最后一行:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public NetworkResponse performRequest(Request<?> request) throws VolleyError {  
  2.         long requestStart = SystemClock.elapsedRealtime();  
  3.         while (true) {  
  4.             HttpResponse httpResponse = null;  
  5.             byte[] responseContents = null;  
  6.             Map<String, String> responseHeaders = new HashMap<String, String>();  
  7.             try {  
  8.                 // Gather headers.  
  9.                 Map<String, String> headers = new HashMap<String, String>();  
  10.                 addCacheHeaders(headers, request.getCacheEntry());  
  11.                 httpResponse = mHttpStack.performRequest(request, headers);  

c)一个Cache对象,这是一个缓存对象,Volley中默认的实现类是DiskBasedCache类。
第二步:创建好RequestQueue之后,就会调用其start方法。而在其start方法呢,就会去创建两个线程,就是图上面的CacheDispatcher和NetworkDispatcher了,如下:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void start() {  
  2.     stop();  // Make sure any currently running dispatchers are stopped.  
  3.     // Create the cache dispatcher and start it.  
  4.     mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);  
  5.     mCacheDispatcher.start();  
  6.   
  7.   
  8.     // Create network dispatchers (and corresponding threads) up to the pool size.  
  9.     for (int i = 0; i < mDispatchers.length; i++) {  
  10.         NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork,  
  11.                 mCache, mDelivery);  
  12.         mDispatchers[i] = networkDispatcher;  
  13.         networkDispatcher.start();  
  14.     }  
  15. }  

当然,也需要让这两个线程跑起来,我们也可以在这里看到,在Volley类中创建的Network接口对象,Cache接口对象,还有一个包装了Handler类的对象mDelivery都会在这里作为参数传给CacheDispatcher和NetworkDispatcher,当然,最重要的,也会将RequestQueue这个装请求的队列给传进去,而这两个线程一旦开始之后,就会一直处理或者等待请求。
第三步:如果Cache中没有记录的话,还是需要通过NetworkDispatcher去实现的,所以接下来就是Network在后面做事情了,看下面的代码:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void run() {  
  2.     Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);  
  3.     Request<?> request;  
  4.     while (true) {  
  5.             ...  
  6.             // Perform the network request.  
  7.             NetworkResponse networkResponse = mNetwork.performRequest(request);  
  8.             request.addMarker("network-http-complete");  

其实也就是调用Network去发送请求,而Network又会调用HttpStack去发送请求。
第四步:通过Handler将Response给传回主线程中定义的Listener,
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. mDelivery.postResponse(request, response);         


而mDelivery其实是一个ExecutorDelivery对象,它封装了Handler,通过它,会调用到请求的deliverResponse方法,如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. // Deliver a normal response or error, depending.  
  2. if (mResponse.isSuccess()) {  
  3.     mRequest.deliverResponse(mResponse.result);  
  4. else {  
  5.     mRequest.deliverError(mResponse.error);  
  6. }  

而每个具体的请求都需要去实现自己的逻辑,比如在ImageRequest中,
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private final Response.Listener<Bitmap> mListener;  
  2.   
  3. @Override  
  4. protected void deliverResponse(Bitmap response) {  
  5.     mListener.onResponse(response);  
  6. }  

我们可以看到,它其实也就是调用Listener中的onResponse方法,而Listener的这个方法,则是我们在创建Request对象的时候,需要去实现的一个方法,所以一切都跑回到
主线程中方法去了。
嗯,隐藏在Volley后面大概的流程就是这样。结束。
0 0