0

Im using REST in my webservice. This is the example payload/parameter i sent when i test my webservice using advance rest client, with form content type (this is the RAW type):

child_id%5B%5D=1&user_id=15&group_id=1&custom=&time=17%3A17%3A00&date=&child_id%5B%5D=2

Where child_id%5B%5D in RAW means child_id[] in the form, so i send an array of child_id (left most and right most in above form). Things are fine here.

The problem occured when i tried to implement this to the Android apps, where i POST those payload/parameter using a HashMap. HashMap cant store two values under the same key, so i cant do something like :

map.put("child_id[]", 1);
map.put("child_id[]", 2);

The latest put will overwrite the earlier put, so the Android apps will only send 2 as value of the child_id.

What should i do? Thanks for your help

7
  • Have you considered using a Collection? Commented Oct 17, 2014 at 4:50
  • @Justin what do you mean by collection? Is it Arraylist? Commented Oct 17, 2014 at 4:52
  • What about using JSON as a data exchange format ? Android has built-in support for JSON parsing. Commented Oct 17, 2014 at 4:54
  • 1
    did you try to add index to the array? i.e child_id[0] and child_id[1] so on. Commented Oct 17, 2014 at 4:55
  • 1
    @BlazeTama Yes, sorry. Technically a HashMap is also a Collection. An ArrayList is probably what you want. Commented Oct 17, 2014 at 4:56

2 Answers 2

2

You Could add index to the array as follows.

map.put("child_id[0]", 1);
map.put("child_id[1]", 2);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, this is the easiest way in my case
1

You have a few options for solving this. The easiest would be to create a class to act as a container for your parameters, and store those in an ArrayList

public class Parameter {
    private String key;
    private String value;
    // Getters, setters, other stuff.
}

Which you would then store in an ArrayList

You could also make a class to act as a parameter builder of sorts. You might do something like this:

public class Parameters {
    private ArrayList<String> keys = new ArrayList<>();
    private ArrayList<String> values = new ArrayList<>();

    public void add(String key, String value) { ... }
    public ArrayList<String> getKeys() { ... }
    public ArrayList<String> getValues() { ... }
}

The second option requires a bit more code but adds some extra flexibility and should make for a cleaner API.

Alternatively, as you mentioned in your own comment, you have the option of using a Map<String, ArrayList<String>>. Which would also work very well in place of the ArrayLists in the above example.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.