0

I am using asynchttpclient to do a POST and am constructing a body like this:

{
  "params": {
                  "firstname": "%Paul%"
            }
}

The following sometimes produces the right body, sometimes produces an empty body:

 String encodedFirstname = "%" + first + "%";
 JSONObject paramsVal = new JSONObject();
 paramsVal.put("firstname", encodedFirstname);
 String[] keys = { "params" };
 JSONObject postBody = new JSONObject(paramsVal, keys);

What do I need to correct? Thank you.

HukeLau_DABA
  • 2,384
  • 6
  • 33
  • 51
  • Construct an empty object and `put` the other object into it, rather than trying to construct it with the elements in it. (Reasonably clear from the documentation.) – Hot Licks Aug 17 '14 at 19:10
  • Nope, the javadoc has a constructor with jsonObject as the first argument. Ambiguous. – HukeLau_DABA Aug 17 '14 at 19:15
  • But if you look at the documentation, that constructor *extracts* values from the supplied jsonObject -- it doesn't insert the jsonObject in the new jsonObject. – Hot Licks Aug 17 '14 at 19:24
  • "Construct a JSONObject from a subset of another JSONObject." - unclear, even after your explanation I don't know what this ctor overload does. – HukeLau_DABA Aug 17 '14 at 19:31
  • Exactly what it says: Constructs the new object from the specified parts of the old one. – Hot Licks Aug 17 '14 at 19:37

2 Answers2

0

This works for me:

JSONObject object1 = new JSONObject();
try {
        object1.put("firstname", "%Paul%");
} catch (JSONException e) {
        e.printStackTrace();
}
JSONObject object2 = new JSONObject();
try {
        object2.putOpt("params", object1);
} catch (JSONException e) {
        e.printStackTrace();
}
Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
Fady Ibrahim
  • 376
  • 4
  • 10
-1

I tried using put again and it worked:

JSONObject postBody = new JSONObject("params", paramsVal);

Worst. Javadoc. Ever.

HukeLau_DABA
  • 2,384
  • 6
  • 33
  • 51