android volley如何发送数组?
android volley how to send array?
所以这是我的基本 getParams post 方法,但我不知道如何将数组发送到后端有人可以帮忙吗?
@Override
protected Map<String, String> getParams() {
JSONObject jsonObject = new JSONObject();
//looping throught recyclerview
for (int i = 0; i < CustomCreateGroupAdapter.dataModelArrayList.size(); i++){
//getting selected items
if(CustomCreateGroupAdapter.dataModelArrayList.get(i).getSelected()) {
try {
//putting all user ids who you selected into jsonObject
jsonObject.put("params", CustomCreateGroupAdapter.dataModelArrayList.get(i).getOthersid());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Map<String, String> params = new HashMap<String, String>();
params.put("params",jsonObject.toString());
return params;
}
您应该将所有这些值添加到 JSONArray,然后将此 JSONArray 添加到您的 JSONObject。您还可以将所有对象添加到一个简单的数组中,然后通过调用 new JSONArray(your_array);
获取相应的 JSONArray
将您的负载对象添加到 JSONArray, then use JSONArray.toString()
to pass the payload to a JsonRequest 作为 requestBody
。
所以这是我的基本 getParams post 方法,但我不知道如何将数组发送到后端有人可以帮忙吗?
@Override
protected Map<String, String> getParams() {
JSONObject jsonObject = new JSONObject();
//looping throught recyclerview
for (int i = 0; i < CustomCreateGroupAdapter.dataModelArrayList.size(); i++){
//getting selected items
if(CustomCreateGroupAdapter.dataModelArrayList.get(i).getSelected()) {
try {
//putting all user ids who you selected into jsonObject
jsonObject.put("params", CustomCreateGroupAdapter.dataModelArrayList.get(i).getOthersid());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Map<String, String> params = new HashMap<String, String>();
params.put("params",jsonObject.toString());
return params;
}
您应该将所有这些值添加到 JSONArray,然后将此 JSONArray 添加到您的 JSONObject。您还可以将所有对象添加到一个简单的数组中,然后通过调用 new JSONArray(your_array);
将您的负载对象添加到 JSONArray, then use JSONArray.toString()
to pass the payload to a JsonRequest 作为 requestBody
。