Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley send Array as a param along with String

I am using Volley to send data to the server, Here I am unable to find the way to send both String and Array at a time in a single request.

I can send the array like:

Map<String, List<String>> jsonParams = new HashMap<>();
jsonParams.put("names", my_names_list);

And also I can send String like:

Map<String, String> jsonParams = new HashMap<>();
jsonParams.put("user_id", userId);

but how to send both at a time?

like:

Map<String, String> jsonParams = new HashMap<>();
jsonParams.put("user_id", userId);
jsonParams.put("names", my_names_list); // here it is expecting String but I want to send array of strings to server

the expected JSON request should be like:

{
"user_id": "1",
"names" : ["abc, "cdf", "efg"]
}
like image 461
Shailendra Madda Avatar asked Nov 21 '25 04:11

Shailendra Madda


1 Answers

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;   

}

like image 82
Rishabh Rawat Avatar answered Nov 22 '25 19:11

Rishabh Rawat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!