Reputation: 2998
I've got a random issue that I cannot seem to work out. Given the following code:
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("method","getProducts");
HashMap<String, String> methodParams = new HashMap<String, String>();
methodParams.put("currency", currency);
params.put("params", methodParams);
restClient.setBodyValueForKey("json", new JSONObject(params).toString());
The following hashmaps are converted to JSON and sent to a PHP server.
The server should receive the data in the following format via POST
Array
(
[json] => {"method":"getProducts","params":{"currency":"GBP"}}
)
On some android devices (not sure what the determining factor is) this is correct but on others it is being sent as
Array
(
[json] => {"method":"getProducts","params":"{currency=GBP}"}
)
As you can see the second hash map is being converted into a string and not being added as a HashMap
Does anyone know what could be causing the inconsistency in parsing the HashMaps?
Thanks
Upvotes: 2
Views: 1626
Reputation: 21
The problem is with the wrap function which is inside JSONObject class. As it works >= API 19. That's why it not able to convert properly. You can use GSON for that.
Upvotes: 0
Reputation: 2118
Try doing the following
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("method","getProducts");
HashMap<String, String> methodParams = new HashMap<String, String>();
methodParams.put("currency", currency);
params.put("params", new JSONObject(methodParams));
restClient.setBodyValueForKey("json", new JSONObject(params).toString());
UPDATE
// replace
params.put("params", new JSONObject(methodParams).toString());
//with
params.put("params", new JSONObject(methodParams));
Upvotes: 2