0

I need to do Volley POST method with this kind of body for onResponse method:

{"table":{"ma":1,"mb":2},"token":"access"}.

So basically I need HashMap called "params" that takes multiple key-value pairs as values for "table" key, and single value for"token" key, that goes inside Volley request.

I tried to do this,but it requires List for second argument for "token" value

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

    // Hash map for "ma" 
    HashMap<String, String> maParams = new HashMap<>();
    maParams.put("ma", "1");

    Hash map for "mb"
    HashMap<String, String> mbParams= new HashMap<>();
    mbParams.put("mb", "2");

    // Table values
    List<String> values = new ArrayList<>();
    values.add(String.valueOf(maParams));
    values.add(String.valueOf(mbParams));

    params.put("table", beacons);
    params.put("token", "access");

Any ideas how I can do that? Thanks in advance.

alezniki
  • 185
  • 1
  • 14

1 Answers1

1

What you require in your body actually looks like JSON. As I don't actually understand what you are trying to achieve with your code I would instead suggest that you take a look to famous JSON APIs such as Jackson or give a try to JSONObject . You will easily find ways to build an object just like you asked {"table":{"ma":1,"mb":2},"token":"access"}.

If overkill for you then just write pure Java to achieve what you want: build a map of tokens (instead of 2 Maps + 1 List) and write a formatting method to put it like you want in your params. Note: as {"ma":1,"mb":2} is not double-quoted in your example then what params must look like is more certainly a simple String (and not any kind of Map!). Actually this is JSON..

EDIT: you are strongly advised to have a look to these posts:

Which all look like possible duplicates..

bsaverino
  • 1,221
  • 9
  • 14
  • Thanks bsaverino, what I basically want to do is to make HashMap out of that JSON file, to use it for Volley POST Request. – alezniki Nov 07 '17 at 11:27
  • Edited my answer as Google and StackOverflow can already answer for me. All you need is quite common and just there. – bsaverino Nov 08 '17 at 01:50
  • And be careful to NOT mix up the parameters Map and your JSON objects (or body). This is not the same! `List` as params values is not very common and sounds illegal to me. Parameters should remain a `Map` somehow. – bsaverino Nov 08 '17 at 01:59