CenKa
CenKa

Reputation: 15

android volley how to send array?

So heres my stuff basic getParams post method in volley but I don't know how to send array to backend can someone help?

@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;
    }

Upvotes: 0

Views: 198

Answers (2)

Submersed
Submersed

Reputation: 8870

Add the objects of your payload to a JSONArray, then use JSONArray.toString() to pass the payload to a JsonRequest as the requestBody.

Upvotes: 0

B. Pl&#252;ster
B. Pl&#252;ster

Reputation: 654

You should add all those values to a JSONArray and then add this JSONArray to your JSONObject. You could also add all objects to a simple array and then get the corresponding JSONArray by calling new JSONArray(your_array);

Upvotes: 1

Related Questions