volley源码解析(七)--最终目的之Response<T>

本文详细解读了Volley如何通过包装HttpResponse为NetworkResponse来提高网络请求的灵活性,并阐述了如何利用NetworkResponse进行响应解析,进一步封装为统一的Response对象。文章还探讨了解决内存溢出问题的方法,以及如何通过改进Volley实现更完善的图片加载功能。最后,展望了Volley的未来发展方向,包括对其现有功能的改造与增强。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

BasicNetwork代码片段:

 // Some responses such as 204s do not have content.  We must check.  
                if (httpResponse.getEntity() != null) {//返回响应主体  
                  responseContents = entityToBytes(httpResponse.getEntity());//将主体转换byte[]形式  
                } else {//没有返回内容  
                  // Add 0 byte response as a way of honestly representing a  
                  // no-content request.  
                  responseContents = new byte[0];  
                }  
这样可能造成的一个问题,就是内存溢出,这也是Volley之所以不能用来下载大文件的原因,因为byte[]是保存在内存中的。


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

     /** 
     * The HTTP status code.
     * http状态码 
     */
    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).
     * 网页是否修改.304 
     */
    public final boolean notModified;

    /** 
     * Network roundtrip time in milliseconds.
     * 响应时间 
     */
    public final long networkTimeMs;

/**
     * 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
     * @param networkTimeMs Round-trip network time to receive network response
     */
    public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers,
            boolean notModified, long networkTimeMs) {
        this.statusCode = statusCode;
        this.data = data;
        this.headers = headers;
        this.notModified = notModified;
        this.networkTimeMs = networkTimeMs;
    }

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


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

NetworkDispatcher代码片段:

// Parse the response here on the worker thread. 解析网络响应到本地
                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,使之有更多更完善的功能。


curl --digest --compressed -u admin:PASSWORD http://169.254.0.1/import/config -X POST --form-string 'configrecord=<?xml version="1.0" standalone="yes"?> <!-- Automatically generated XML --> <!DOCTYPE configrecord [ <!ELEMENT configrecord (configgroup+)> <!ELEMENT configgroup (configitem+)> <!ELEMENT configitem (value+)> <!ELEMENT value (#PCDATA)> <!ATTLIST configrecord version CDATA #IMPLIED> <!ATTLIST configgroup name CDATA #IMPLIED> <!ATTLIST configgroup instance CDATA #IMPLIED> <!ATTLIST configitem name CDATA #IMPLIED> <!ATTLIST configitem instance CDATA #IMPLIED> <!ATTLIST value name CDATA #IMPLIED> ]> <configrecord version = "0.1.0.1"> <configgroup name = "Radio"> <configitem name = "Mode"> <value>Enabled</value> </configitem> <configitem name = "Band"> <value>5 GHz Only</value> </configitem> <configitem name = "Antenna Selection"> <value>Auto</value> </configitem> <configitem name = "Keep Alive"> <value>Enabled</value> </configitem> <configitem name = "Max Volley Delay"> <value>5 seconds</value> </configitem> <configitem name = "TX Power Maximum"> <value>19 dBm</value> </configitem> <configitem name = "Log"> <value>Disabled</value> </configitem> <configitem name = "Event Log"> <value>Disabled</value> </configitem> <configitem name = "Roaming"> <value name = "State">Disabled</value> <value name = "Scan Period">5 seconds</value> <value name = "Band 2.4GHz Trigger Delta">5 dBm</value> <value name = "Band 2.4GHz RSSI Floor">-65 dBm</value> <value name = "Band 5GHz Trigger Delta">5 dBm</value> <value name = "Band 5GHz RSSI Floor">-65 dBm</value> </configitem> <configitem name = "Scanning"> <value name = "RSSI Floor">-60 dBm</value> </configitem> </configgroup> </configrecord>' 将以上代码加载到一个shel脚本中 执行脚本就执行此命令
最新发布
07-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值