Android Volley send Array as a param along with String
Asked Answered
C

4

3

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"]
}
Corrinacorrine answered 23/4, 2018 at 13:39 Comment(0)
D
2

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;   

}

Doroteya answered 23/4, 2018 at 14:7 Comment(4)
Thank you for the suggestion, I'll do it nowCorrinacorrine
Thank you Shlendra i am just 3rd year computer science student.Doroteya
Great, I know this process before but the question is different I wanna send String and Array in Map in Volley without using Gson, but for now I went with the solution as you suggestedCorrinacorrine
please help me #51206758Sellars
B
1

I solved my problem like this-

protected Map<String, String> getParams() throws AuthFailureError {
            JSONArray words_ar = new JSONArray();
            for(int i=0;i<exercise.getWord_list().size();i++){
                JSONObject word_ob = new JSONObject();
                try {
                    word_ob.put("id",(i+1)+"");
                    word_ob.put("learn",exercise.getWord_list().get(i).getLearn_str());
                    word_ob.put("native",exercise.getWord_list().get(i).getNative_str());
                    words_ar.put(word_ob);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            Map<String, String> params = new HashMap<>();
            params.put("user", prefs.getUserId()+"");
            params.put("title", exercise.getTitle());
            params.put("words", words_ar+"");
            return params;
        }
Birdiebirdlike answered 23/4, 2018 at 14:6 Comment(11)
you're welcome. Hopefully this may resolve your problem.Birdiebirdlike
but I don't have any key-value pairs for my array list, pls check my updated questionCorrinacorrine
Try - JSONArray words_ar = new JSONArray(your_list);Birdiebirdlike
Okay let me checkCorrinacorrine
If I did like you said JSONArray words_ar = new JSONArray(your_list); but am getting the list in double quotes like "["asdf"]" in the request.Corrinacorrine
I tried iterating map as - for (Map.Entry<String,String> entry : params.entrySet()) System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); and it gives me an arrayBirdiebirdlike
Well in PHP this request can be easily decoded with the help of json_decode()Birdiebirdlike
Let us continue this discussion in chat.Corrinacorrine
@Shylendra Medda I have written something in that chat have you checked that.Birdiebirdlike
Yeah, I replied too.Corrinacorrine
its not work for me please help me #51206758Sellars
D
0

Try using custom JSONObject. For your above example request, you can create a JSON object as below

    try {
        String[] names = {"abc", "cdf", "efg"};
        String userId = "user_id";
        JSONArray jsonArray = new JSONArray();
        for(String n : names) {
            jsonArray.put(n);
        }

        JSONObject jsonParams = new JSONObject();
        jsonParams.put("names", jsonArray);
        jsonParams.put("userId", userId);
    } catch (JSONException e) {
        e.printStackTrace();
    }
Digital answered 23/4, 2018 at 14:9 Comment(1)
but I don't have any key-value pairs for my array list, pls check my updated questionCorrinacorrine
E
0

try this one ; put the names in array than:

for(int i = 0;i<yourArrayLength;i++){

params.put("names"+i, my_names_list[i]);

}

hope it will help

Epiphenomenalism answered 23/4, 2018 at 16:26 Comment(2)
sorry but its not working in my case #51206758Sellars
please help me if you know how can i send list of string in the params of volley androidSellars

© 2022 - 2024 — McMap. All rights reserved.