Volley 框架自定义请求(Request)之参数传递

Volley自定义请求详解
本文详细介绍Volley框架中自定义请求(Request)的方法及参数传递技巧,包括GET和POST请求的具体实现。

一、前言

    Volley 是 Android 官方的 HTTP 请求框架 ,它是基于 HttpURLConnection 的,OKHttp 是基于 WebSocket 的,它有一些优势,也有一些缺点,更多关于 Volley的使用,可以参考官方文档:使用 Volley 传输网络数据

二、Volley 自定义请求(Request)

    使用过 Volley 的都知道,Volley 已经提供了一些请求(Request)的实现,他们分别是:

  • StringRequest:请求返回的数据是 String 类型
  • JsonObjectRequest:请求返回的数据是 JSONObject 类型
  • JsonArrayRequest:请求返回的数据是 JSONArray 类型
  • ImageRequest:请求返回的数据是图片

2.1 自定义请求(Request)简要

    一般来说,以上请求类型可以满足基本需求,如果不满足,开发者也可以根据自己的需求自定义请求(Request)。自定义请求可以继承框架现有的请求类(比如 StringRequest,可以通过继承他实现 POST 请求传递参数),也可以直接继承 Request 抽象类,自定义请求主要需要关注以下一些方法。

  • parseNetworkResponse(NetworkResponse response):解析网络返回的元数据(字节数组),并返回目标数据类型的数据。如果直接继承 Request 抽象类,这个方法必须重写。
  • deliverResponse(T response):分发相应数据,将解析的目标数据分发回调(可在此做一些相应的数据处理)。如果直接继承 Request 抽象类,这个方法必须重写。
  • getHeaders():返回请求需要添加的额外 HTTP 头数据(若提供这些值需要认证,有可能会抛出 AuthFailureError),重写该方法可以往请求中添加额外的 HTTP 请求头参数。
  • getParams():返回请求参数映射表,只对 POST 或 PUT 类型的请求有效(若提供这些值需要认证,有可能会抛出 AuthFailureError),重写该方法可以往POST 和 PUT 请求中传递参数。
  • getParamsEncoding():返回参数编码类型,Volley 默认是 utf-8 编码。重写此方法可以改变参数编码类型。
  • getBody():返回 POST 或 PUT 请求需要发送的请求体元数据(字节数组)。Request 类默认实现是针对 HTTP form 结构(application/x-www-form-urlencoded; charset=utf-8,即 key1=values1&key2=value2 表单结构),如果需要传入其他结构的数据,可以重写此方法,但是注意需要与 getBodyContentType() 方法同步修改,保持一致。
  • getBodyContentType():返回 POST 或 PUT 请求体的内容类型,对应请求头的 Content-Type。重写此方法,可改变请求体的数据结构,但是需要确保 getBody() 方法返回的请求体内容的结构与之对应。

说明:更多详细说明可以参考 Volley 源码中 Request 类的声明与注释,Volley Github项目

2.2 通过源码剖析讲解自定义请求(Request)

    下面,将使用 JsonObjectRequest 源码详细说明下自定义请求。

我们先来看看 JsonObjectRequest 的构造函数,其中参数 jsonRequest 的注释说明是需要通过请求发送的参数数据(为空时表示无参数),因为 JsonObjectRequest 不是直接继承自 Request 类,而是中间还有一层继承 JsonRequest,所以,我们同时看看 JsonObjectRequest 的构造函数中通过 super 调用父类的构造函数,需要注意的是,请求体数据类型被转换为 JSONObject 格式的字符串。

public class JsonObjectRequest extends JsonRequest<JSONObject> {
   
   
    /**
     * Creates a new request.
     *
     * @param method the HTTP method to use
     * @param url URL to fetch the JSON from
     * @param jsonRequest A {@link JSONObject} to post with the request. Null indicates no
     *     parameters will be posted along with request.
     * @param listener Listener to receive the JSON response
     * @param errorListener Error listener, or null to ignore errors.
     */
    public JsonObjectRequest(
            int method,
            String url,
            @Nullable JSONObject jsonRequest,
            Listener<JSONObject> listener,
            @Nullable ErrorListener errorListener) {
   
   
        super(
                method,
                url,
                jsonRequest != null ? jsonRequest.toString() : null,
                listener,
                errorListener);
    }
}

public abstract class JsonRequest<T> extends Request<T> {
   
   
    /**
     * Creates a new request.
     *
     * @param method the HTTP method to use
     * @param url URL to fetch the JSON from
     * @param requestBody The content to post as the body of the request. Null indicates no
     *     parameters will be posted along with request.
     * @param listener Listener to receive the JSON response
     * @param errorListener Error listener, or null to ignore errors.
     */
    public JsonRequest(
            int method,
            String url,
            @Nullable String requestBody,
            Listener<T> listener,
            @Nullable ErrorListener
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值