volley介绍08

-----------------------------------------------------------------------------------

转载:http://blog.csdn.net/crazy__chen/article/details/46612901

-----------------------------------------------------------------------------------

在上篇文章中,我们最终通过网络,获取到了HttpResponse对象

HttpResponse是android包里面的一个类,然后为了更高的扩展性,我们在BasicNetwork类里面看到,Volley将其包装成一个Volley自己的对象NetworkResponse

另外,在BasicNetwork类中我们也注意到,对HttpResponse包装成NetworkResponse的过程中,使用HttpResponse的Inputstream,将数据保存在一个byte[]数组中。

BasicNetwork代码片段:

[java] view plain copy
 
  1. // Some responses such as 204s do not have content.  We must check.    
  2.                if (httpResponse.getEntity() != null) {//返回响应主体    
  3.                  responseContents = entityToBytes(httpResponse.getEntity());//将主体转换byte[]形式    
  4.                } else {//没有返回内容    
  5.                  // Add 0 byte response as a way of honestly representing a    
  6.                  // no-content request.    
  7.                  responseContents = new byte[0];    
  8.                }    

这样可能造成的一个问题,就是内存溢出,这也是Volley之所以不能用来下载大文件的原因,因为byte[]是保存在内存中的。

好了,下面让我们来看NetworkResponse的源码

[java] view plain copy
 
  1.      /**  
  2.      * The HTTP status code. 
  3.      * http状态码  
  4.      */  
  5.     public final int statusCode;  
  6.   
  7.     /**  
  8.      * Raw data from this response. 
  9.      * 数据  
  10.      */  
  11.     public final byte[] data;  
  12.   
  13.     /**  
  14.      * Response headers. 
  15.      * 响应头  
  16.      */  
  17.     public final Map<String, String> headers;  
  18.   
  19.     /**  
  20.      * True if the server returned a 304 (Not Modified). 
  21.      * 网页是否修改.304  
  22.      */  
  23.     public final boolean notModified;  
  24.   
  25.     /**  
  26.      * Network roundtrip time in milliseconds. 
  27.      * 响应时间  
  28.      */  
  29.     public final long networkTimeMs;  
  30.   
  31. /** 
  32.      * Creates a new network response. 
  33.      * @param statusCode the HTTP status code 
  34.      * @param data Response body 
  35.      * @param headers Headers returned with this response, or null for none 
  36.      * @param notModified True if the server returned a 304 and the data was already in cache 
  37.      * @param networkTimeMs Round-trip network time to receive network response 
  38.      */  
  39.     public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers,  
  40.             boolean notModified, long networkTimeMs) {  
  41.         this.statusCode = statusCode;  
  42.         this.data = data;  
  43.         this.headers = headers;  
  44.         this.notModified = notModified;  
  45.         this.networkTimeMs = networkTimeMs;  
  46.     }  


本质上没有什么特别的,只是将HttpResponse的内容,简单地转移到NetworkResponse中

接下来,在响应分发过程中,request负责把NetworkResponse又包装成Response<T>对象

NetworkDispatcher代码片段:

[java] view plain copy
 
  1. // Parse the response here on the worker thread. 解析网络响应到本地  
  2.                 Response<?> response = request.parseNetworkResponse(networkResponse);  

至于怎么解析,不同的request应该有自己的实现。

可能看到这里大家有些迷糊,原因是我们找回了之前类的一些代码

在前面的解析中,我们总是忽略这些片段,默认为全都是Response,因为在前面的过程中,理解Response之间的不同会给我们理解核心代码带来困扰,所以我们都跳过了。

现在源码解析接近尾声,我们再回头看各种各样的Response就豁然开朗了。

httpStack获得的是HttpResponse,由于HttpResponse是android的内置类,我们使用起来非常不灵活(因为我们希望response都是一样的,无论是从缓存中取的还是网络请求的)

根据上述原因,我们有了NetworkResponse,这个代表网络请求相应,这是Volley的自定义类,这样我们使用起来就灵活了(理论上缓存也应该有一个CacheResponse,然而Volley没有这样设计)。更加重要的一点是NetworkResponse中的byte[]数组保存了网络数据(前面说过,这是造成内存溢出的原因)

最后,为了统一所有的Response,我们将NetworkResponse(理论上还有一个CacheResponse)又封装成了了Response<T>

OK,Volley解析基本到这里就结束了。接下来的文章,将会带大家看一下Volley最后的一部分小花絮,关于图片加载的部分。

另外,我还会根据自己的理解,带大家来改造Volley,使之有更多更完善的功能。

原文地址:https://www.cnblogs.com/aprz512/p/5316746.html