Android Volley将Array和String一起作为参数发送 [英] Android Volley send Array as a param along with String
本文介绍了Android Volley将Array和String一起作为参数发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Volley 将数据发送到服务器,在这里无法找到在单个请求中一次发送String和Array的方法.
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);
我也可以像这样发送String:
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?
喜欢:
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
预期的JSON请求应类似于:
the expected JSON request should be like:
{
"user_id": "1",
"names" : ["abc, "cdf", "efg"]
}
推荐答案
我认为您可以将字符串和数组合并为单个json,然后将json发送到服务器.示例
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
}
使用GSON库将该模型类制作为json.
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;
}
这篇关于Android Volley将Array和String一起作为参数发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文