Android volley post 字符串以外的参数
Android volley post params other than string
我正在使用 Volley 库来满足我的请求。我创建了一个自定义 class,它像这样扩展 Request<T>
:
class CustomRequest<T> extends Request<T>
每个使用 GET 的请求都运行良好。
我有一些使用 POST 方法的请求。比如login需要login和pwd,所以我把Map放到class的getParams()里面,一切正常。但是我在另一个请求上遇到了问题,因为我需要将参数放入任何类型的值,如 int、array 等......这里的示例是设法在 POST 的请求中传递它:
{"items":[12926315]}
所以,我该怎么做,因为 Volley 只接受 Map,我当然会得到一个 400 错误代码。
非常感谢您的帮助!
public class StringRequest extends Request<String> {
private final Listener<String> mListener;
/**
* Creates a new request with the given method.
*
* @param method the request {@link Method} to use
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequest(int method, String url, Listener<String> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
/**
* Creates a new POST request.
*
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
this(Method.POST, url, listener, errorListener);
}
@Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}
希望这会很好。
编辑
所以如果我像下面那样使用 JsonObjectRequest
,并传递 JSONObject POST 数据,它工作得很好,但我想 POST 我的 JSONObject 在 getBody
of Volley in my class which extends Request<T>
但它不起作用,因为我猜 JSONObject 被强制转换并添加一些引号
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, dataToPost,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("LOG", "+++++++++ success");
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("LOG", "--------- error");
}
})
{
};
这是我的 JSONObject:
JSONObject dataToPost = new JSONObject();
JSONArray jsa = new JSONArray();
jsa.put(12926315);
jsa.put(12998018);
dataToPost.put("items", jsa);
这里是 getBody 方法:
public byte[] getBody() throws AuthFailureError {
return postJson.toString().getBytes(); // postJson is dataToPost
}
但是从服务器端,响应是 {"items":'[12926315, 12998018]'} 等待 {"items":[12926315, 12998018]}
有什么想法吗?
我刚刚发现它为什么不起作用,我需要重写 getBodyContentType()
方法
public String getBodyContentType()
{
return "application/json";
}
现在可以了!希望这会有所帮助:)
我正在使用 Volley 库来满足我的请求。我创建了一个自定义 class,它像这样扩展 Request<T>
:
class CustomRequest<T> extends Request<T>
每个使用 GET 的请求都运行良好。
我有一些使用 POST 方法的请求。比如login需要login和pwd,所以我把Map放到class的getParams()里面,一切正常。但是我在另一个请求上遇到了问题,因为我需要将参数放入任何类型的值,如 int、array 等......这里的示例是设法在 POST 的请求中传递它:
{"items":[12926315]}
所以,我该怎么做,因为 Volley 只接受 Map,我当然会得到一个 400 错误代码。
非常感谢您的帮助!
public class StringRequest extends Request<String> {
private final Listener<String> mListener;
/**
* Creates a new request with the given method.
*
* @param method the request {@link Method} to use
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequest(int method, String url, Listener<String> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
/**
* Creates a new POST request.
*
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
this(Method.POST, url, listener, errorListener);
}
@Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}
希望这会很好。
编辑
所以如果我像下面那样使用 JsonObjectRequest
,并传递 JSONObject POST 数据,它工作得很好,但我想 POST 我的 JSONObject 在 getBody
of Volley in my class which extends Request<T>
但它不起作用,因为我猜 JSONObject 被强制转换并添加一些引号
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, dataToPost,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("LOG", "+++++++++ success");
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("LOG", "--------- error");
}
})
{
};
这是我的 JSONObject:
JSONObject dataToPost = new JSONObject();
JSONArray jsa = new JSONArray();
jsa.put(12926315);
jsa.put(12998018);
dataToPost.put("items", jsa);
这里是 getBody 方法:
public byte[] getBody() throws AuthFailureError {
return postJson.toString().getBytes(); // postJson is dataToPost
}
但是从服务器端,响应是 {"items":'[12926315, 12998018]'} 等待 {"items":[12926315, 12998018]}
有什么想法吗?
我刚刚发现它为什么不起作用,我需要重写 getBodyContentType()
方法
public String getBodyContentType()
{
return "application/json";
}
现在可以了!希望这会有所帮助:)