当我使用邮差,我收到正确的响应(服务器进程“类别”作为阵列)使用以下请求描述从服务器: 使用Volley在POST请求中发送数组的正确方法是什么?
该请求具有这种原始代表:
POST /api/items HTTP/1.1
Host: www1.
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: c18f92ee-2f36-f4dd-2963-6bc6d1db0bdf
items=247642&categories%5B%5D=12&categories%5B%5D=11
我的问题是如何表示描述的请求,以正确使用Volley库发射它。我试过很多变种,比如:
@Override
public byte[] getBody() throws AuthFailureError {
return "items=247642&categories%5B%5D=12".getBytes();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = super.getHeaders();
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
但他们要么都不编码“类别”为阵列(这样服务器不参与帐户在所有这些变量)或作出不正确的方式编码,所以服务器无法获得这个数组的值。
UPDATE:
我只是试着用OkHttp火这一要求,这是绝对正确的(如与情况邮差以上):
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "items=247642&categories%5B%5D=10&categories%5B%5D=9");
Request request = new Request.Builder()
.url("http://www1...")
.post(body)
.addHeader("content-type", "application/x-www-form-urlencoded")
.addHeader("cache-control", "no-cache")
.build();
这意味着只有一件事:Volley以其他方式处理原始请求体(可能是不正确的),或者Volley中存在一些微妙的配置,无法正确执行这些请求。