How to Send Array of Params Using Volley in Android

Android Volley send Array as a param along with String

I think you can merge the string and array into a single json and then send the json to the server.Example

public class Member
{
private String name;
private List<String> skills;

//getter and setter at lower
}

Use the GSON library for make this model class to json.

Member mem = createjsonobject();
Gson gson=new Gson();
String json=gson.toJson(mem);

//Pass this json to the server and at server side you can seperate the string and array

private static Member createjsonObject()
{
Member member= new Member();
member.setName("Rishabh");

List<String> skill=new ArrayList<>();
skill.add("Java");
skill.add("C#");
skill.add("Android");

member.setSkills(skill);
return member;

}

how to send array of params using volley in android

Use

HashMap<String ,String> params=new HashMap<String, String>(7);
for(int i=1;i<=7;i++)
{
params.put("params_"+i, arr[i]);
}

in CustomJobjectRequest class because currently you are using String type as value in Map in CustomJobjectRequest class but sending String[] type when create object of CustomJobjectRequest class.

Edit:

To send all values in single parameter to server use JSONObject.Create a json object using all key-value as:

 JSONObject jsonObject=new JSONObject();
for(int i=1;i<=7;i++)
{
arr[i]="questionId_"+i+"_"+"ans_"+i;
jsonObject.put("params_"+i,arr[i]);
}
HashMap<String ,String> params=new HashMap<String, String>();
params.put("params",jsonObject.toString());

TO send all values on server side get params and convert to JSON object and iterate to get all values

pass array data as parameter volley

For this, you have to make JsonArrayRequest. In Volley there is JsonArrayRequest class use that one for JsonArray request.

This is the method available in JsonArrayRequest class.

public JsonArrayRequest(int method, String url, JSONArray jsonRequest, 
Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
errorListener);
}

May be this will help you:

JSONArray jArrayInput=new JSONArray();
JSONObject jObjectInput=new JSONObject();
jObjectInput.put("code", abc);
jObjectInput.put("code2", cba);
jArrayInput.put(jObjectInput);

JsonArrayRequest request = new JsonArrayRequest(Method.POST, /*Your base url*/, jArrayInput , new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Here success response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Here error response
}
});
MyVolley.getRequestQueue().add(request);

Send array from android and receive at PhP server using Volley

you have to override volley's getParams method inside your getEventDetailRespond method

 @Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
JSONObject params = new JSONObject();
try {
for (int i=0; i <eventIDBeacon.size();i++){
params.put(Config.EVENT_ID, eventIDBeacon.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
return params;
};

that's the way to send params using Volley library..
in your case you have to create this:

 Map<String, ArrayList<>> params = new          
HashMap<String,ArrayList<>>();

where array list is your desired list

How to Pass Array in Body of POST Request Using Volley In Android

This is the solution

StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.SAVE_ITEM,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(SelectedPurchaseActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SelectedPurchaseActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("purchase_no", "87676767");
params.put("supplier", "7");
params.put("date", "2020-12-02");
params.put("p_type", "Cash");
params.put("medicine_id[]","618");
params.put("code[]", "202012011");
params.put("expiry_date[]", "2021-01-09");
params.put("batch_no[]", "1");
params.put("qty[]", "1");
params.put("cost[]", "1234");
params.put("total[]", "2468");
params.put("totalQty", "2");
params.put("subTotal", "2468");
params.put("discount", "3");
params.put("d_type", "%");
params.put("payable", "2394");
params.put("paid", "3");
params.put("due", "2391");
return params;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", token);
return params;
}

};

RequestQueue queue = Volley.newRequestQueue(SelectedPurchaseActivity.this);
queue.add(stringRequest);

android volley how to send array?

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);

how pass an array in parameters in volley library

Use this:

for (int i = 0; i < list.size(); i++)
{
parameters.put("car["+i+"]", list.get(i));
}


Related Topics



Leave a reply



Submit