带有重复参数名称的Google Volley请求 [英] Google Volley request with duplicate parameter names
问题描述
我需要发送带有重复参数名称的POST请求,例如"param = a& param = b".
I need to send a POST request with duplicate parameter names, like "param=a¶m=b".
由于Map不能具有重复的键,因此无法覆盖Request.getParams(),因此只会发送一个值.
Overriding the Request.getParams() does not work since Map cannot have duplicate keys, so only one value would be sent.
我知道我可以重写Request类以使用Map或Map>,但是我想知道是否还有其他方法不需要更改库.
I know I can rewrite the Request class to use a Map or Map>, but I was wandering if there is any other way that would not require altering the library.
谢谢.
PS:我已经在凌空用户组上提出了相同的问题: https://groups.google.com/forum/#!topic/volley-users/tFRclnEbpAk
PS: I have filed the same question on the volley-users group: https://groups.google.com/forum/#!topic/volley-users/tFRclnEbpAk
推荐答案
榕树柯克帕特里克回答了我关于排球用户组的问题 ( https://groups.google.com/d/msg/volley- users/tFRclnEbpAk/uiC2f9nAIgkJ ):
Ficus Kirkpatrick answered my question on the volley-users group (https://groups.google.com/d/msg/volley-users/tFRclnEbpAk/uiC2f9nAIgkJ):
您可以重写getBody(),而无需修改库.
You can override getBody() without having to modify the library.
F
因此,我创建了以下帮助程序类:
So I created the following helper class:
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HttpParams extends HashMap<String, List<String>> {
private static final long serialVersionUID = 1L;
public HttpParams() {
super();
}
public HttpParams(int capacity) {
super(capacity);
}
public HttpParams(Map<String, List<String>> map) {
super(map);
}
public HttpParams(int capacity, float loadFactor) {
super(capacity, loadFactor);
}
/*
* This is the method to use for adding post parameters
*/
public void add(String key, String value) {
if (containsKey(key)) {
get(key).add(value);
}
else {
ArrayList<String> list = new ArrayList<String>();
list.add(value);
put(key, list);
}
}
/**
* Converts the Map into an application/x-www-form-urlencoded encoded string.
*/
public byte[] encodeParameters(String paramsEncoding) {
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, List<String>> entry : entrySet()) {
String key = URLEncoder.encode(entry.getKey(), paramsEncoding);
for (String value : entry.getValue()) {
encodedParams.append(key);
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(value, paramsEncoding));
encodedParams.append('&');
}
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
}
,然后在扩展Request的类中,我重写了getBody():
and then in my class that extends Request I overrided the getBody():
@Override
public byte[] getBody() throws AuthFailureError {
if (mParams != null && mParams.size() > 0) {
return mParams.encodeParameters(getParamsEncoding());
}
return null;
}
这篇关于带有重复参数名称的Google Volley请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!