volley介绍08

-----------------------------------------------------------------------------------java

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

-----------------------------------------------------------------------------------数组

在上篇文章中,咱们最终经过网络,获取到了HttpResponse对象缓存

HttpResponse是android包里面的一个类,而后为了更高的扩展性,咱们在BasicNetwork类里面看到,Volley将其包装成一个Volley本身的对象NetworkResponse网络

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

BasicNetwork代码片断:spa

 

[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[]是保存在内存中的。.net

 

 

好了,下面让咱们来看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中code

 

 

接下来,在响应分发过程当中,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,使之有更多更完善的功能。

相关文章
相关标签/搜索