由于一开始官方介绍 Volley 适合轻量高并发的网络请求场景, 并不推荐用于上传下载, 因此以前只是粗略了解下就浅尝辄止, 并没有在项目中正式使用. 直到最近用到Volley. 于是碰到了一个问题.
使用 JsonObjectRequest 发POST请求时, Volley官方说在getParams(xxx) 方法中传递POST参数是无效的, 需要在构造方法中通过 JsonObject 去传递参数, 类似这样:
Map<String, String> params = new HashMap<String, String>();
params.put("username","hello_world");
params.put("password", "123456");
params.put("sex","1");
JSONObject paramJsonObject = new JSONObject(params);
JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
url,paramJsonObject,
successListener, errorListener){
//...
};
但是后台并不会正常接收到, 除非后台是JsonObject格式去接收并处理客户端这边发出的请求参数. 因为传递到后台的参数是这样的:
{"username":"hello_world","password":"123456","sex":"1"}
我们直接看Volley中 JsonObjectRequest 构造方法的相关源码:
public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
errorListener);
}
上面代码可看出, 是直接将装载参数的 JsonObject进行 toString() 处理, 出来的结果就是我们上面所看到的那样. 最后还是回调
public JsonRequest(int method, String url, String requestBody, Listener<T> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
mRequestBody = requestBody;
}
这个方法, 将参数传到String类型的 requestBody, 实现真正的参数传递.
所以结论是, 直接在 JsonObject 中放参数, 是无效的. 而网上到处传来传去说 JsonObjectRequest 请求中在 getParams(...) 中传参无效, getParams(...) 只针对StringRequest. 但也没有给出实际的方案, 还推荐用JsonObject传参...
其实追溯源码看看 JsonObjectRequest, StringRequest 和 Request 中几个类的关键方法, 我们有2种方法在 JsonObjectRequest 的 POST 请求中传参.
第一种: requestBody
从上面的构造方法看出JsonObject最后还是要toString() 并传给requestBody, 而且 JsonObjectRequest 本身也提供了一个包含 requestBody 的构造方法. 因此可以仿效GET请求的格式这样实现传参:
String requestBody = "username=hello_world&password=123456&sex=1";
JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest
(Request.Method.POST,
url,requestBody,
successListener, errorListener){
// ...
};
第二种: getBody(...) 方法
这种方法跟 requestBody 是有关系的. 因为 requestBody 到最后是传值给 mRequestBody. 而 mRequestBody 则在 JsonRequest 中的 getBody(...) 方法被使用:
@Override
public byte[] getPostBody() {
return getBody();
}
@Override
public byte[] getBody() {
try {
return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
mRequestBody, PROTOCOL_CHARSET);
return null;
}
}
一切参数在 getPostBody() 中被返回, 很明显, 这个方法才是最终返回POST参数给请求的.
如果到了这里还不明白, 整个传参的过程, 请直接看 Request 类的这几个方法:
/**
* Returns the raw POST body to be sent.
*
* @throws AuthFailureError In the event of auth failure
*
* @deprecated Use {@link #getBody()} instead.
*/
@Deprecated
public byte[] getPostBody() throws AuthFailureError {
// Note: For compatibility with legacy clients of volley, this implementation must remain
// here instead of simply calling the getBody() function because this function must
// call getPostParams() and getPostParamsEncoding() since legacy clients would have
// overridden these two member functions for POST requests.
Map<String, String> postParams = getPostParams();
if (postParams != null && postParams.size() > 0) {
return encodeParameters(postParams, getPostParamsEncoding());
}
return null;
}
/**
* Returns the raw POST or PUT body to be sent.
*
* <p>By default, the body consists of the request parameters in
* application/x-www-form-urlencoded format. When overriding this method, consider overriding
* {@link #getBodyContentType()} as well to match the new body format.
*
* @throws AuthFailureError in the event of auth failure
*/
public byte[] getBody() throws AuthFailureError {
Map<String, String> params = getParams();
if (params != null && params.size() > 0) {
return encodeParameters(params, getParamsEncoding());
}
return null;
}
/**
* Returns a Map of POST parameters to be used for this request, or null if
* a simple GET should be used. Can throw {@link AuthFailureError} as
* authentication may be required to provide these values.
*
* <p>Note that only one of getPostParams() and getPostBody() can return a non-null
* value.</p>
* @throws AuthFailureError In the event of auth failure
*
* @deprecated Use {@link #getParams()} instead.
*/
@Deprecated
protected Map<String, String> getPostParams() throws AuthFailureError {
return getParams();
}
/**
* Returns a Map of parameters to be used for a POST or PUT request. Can throw
* {@link AuthFailureError} as authentication may be required to provide these values.
*
* <p>Note that you can directly override {@link #getBody()} for custom data.</p>
*
* @throws AuthFailureError in the event of auth failure
*/
protected Map<String, String> getParams() throws AuthFailureError {
return null;
}
/**
* Converts <code>params</code> into an application/x-www-form-urlencoded encoded string.
*/
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
特别是 encodeParameters(...) 方法, 完全看到无论是JsonObject 还是 MAP 装着数据传进来, 最后都是加工成类似 GET 的 "key=value" 的格式. 同时, 我们也明白 StringRequest 如何通过 getParams() 传参的.
这样就好办了, 所以我们在 JsonObjectRequest 的构造方法里直接重写 getBody() 方法就行了:
final String requestBody = "username=hello_world&password=123456&sex=1";
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST,
url,
successListener, errorListener) {
@Override
public byte[] getBody() {
try {
return requestBody.toString().getBytes("UTF-8");
} catch (Exception e) {
}
return null;
}
};