一、前言
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
Volley自定义请求详解

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

被折叠的 条评论
为什么被折叠?



