Volley Post请求,在Json数组请求中发送Json对象 [英] Volley Post request ,Send Json object in Json array request
问题描述
简单地说,我想使用Volley Post Request将此{"Id":7,"Name":"MyName"}
数据发送到服务器.
To say in simple words i want to send this {"Id":7,"Name":"MyName"}
data To server using Volley Post Request.
它有1个整数和1个字符串,我得到的响应是Jsonarray
It has 1 integer and 1 String and response i get is Jsonarray
我尝试了以下方法,但没有用
I tried Following ways but none are working
-
因为它是json数组请求,所以我无法在参数中发送数据,因为第三个参数仅使用JsonArray,并且我必须发送JsonObject,因此将其保留为空
as it is json array request i cannot send data in argument as 3rd argument only takes JsonArray and i have to send JsonObject so kept it as null
new JsonArrayRequest(Method,Url,JsonArray,ResponseListener,ErrorListner)
我不能将其放在HashMap
中,因为该值的1是整数,并且仅接受字符串
I cannot put it in HashMap
as 1 of the value is integer, and it only accepts string
getparams()方法
getparams() method
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<>();
params.put("Id",7); // <====== This is Invalid
params.put("Name","MyName");
return params;
}
- 我尝试发送getbody方法,但仍然无法正常工作
getbody方法
@Override
public byte[] getBody() {
String body="{\"Id\":7,\"Name\":\"MyName\"}";
return body.getBytes();
}
我可以使用HttpUrlConnection获得响应.
I can get the response using HttpUrlConnection.
还有其他方法可以实现凌空抽射吗?
Is there any other way to achieve it in volley ?
推荐答案
Seems like it was removed in recent volley version but you can easily modify this constructor and add to JsonArrayRequest
.
public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
errorListener);
}
这篇关于Volley Post请求,在Json数组请求中发送Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!