Search code examples
androidjsonandroid-async-httpasynchttpclient

How could i dynamically pass the following data as json to asyncHTTPClient?


{     "Name”: ”ValidateUserName”,     “Message”: {         ”UserName”: ”p”     } }

How could i make these parameters to json format to dynamically pass the parameters to AsyncHTTPClient post method.

Using below method gives a wrong json output

HashMap<String, String> param = new HashMap<String, String>();
JSONObject jsonvalue =new JSONObject();
         try {
            jsonvalue.put("Name","ValidateUserName");
        } catch (JSONException e1) {

            e1.printStackTrace();
        }
         try {
            jsonvalue.put("Message",param);
        } catch (JSONException e1) {

            e1.printStackTrace();
        }

Output: { "Name":"ValidateUserName", "Message":" { UserName=amsecmobileuser } " }

Could anyone say why the output is not as expected.......


Solution

  • You have to create another JSONObject for Message. try this:

        HashMap<String, String> param = new HashMap<String, String>();
        param.put("username", "p");
        JSONObject jsonvalue = new JSONObject();
        try {
            jsonvalue.put("Name", "ValidateUserName");
        } catch (JSONException e1) {
    
        e1.printStackTrace();
        }
        try {
            JSONObject messageObj = new JSONObject();
            messageObj.put("UserName", param.get("username"));
            jsonvalue.put("Message", messageObj);
        } catch (JSONException e1) {
    
            e1.printStackTrace();
        }