Sending JSONObject through HTTPpost in a defined format?

Go To StackoverFlow.com

1

     {
     who: 'troll_new3',
     visibility: 'public',

     poll: {
     title: 'Favourite game?',
     des_content_type: 'text',
     description: 'I want to know which game you like!', 
     options_type: 'text',
     comments_enabled: true,  
     poll_method: 'list',     
     }   
    }

I need this as the format of JSONObject which is to be recieved.

This is the code I am implementing:

obj=new JSONObject();
try {
    obj.put("options_type", "text");        
    obj.put("description", pd.getText().toString());
    obj.put("des_content_type", "text");
    obj.put("title", pt.getText().toString());
    obj.put("comments_enabled", "false");
    obj.put("poll_method",str);

} catch (JSONException e1) {
    //TODO Auto-generated catch block
    Log.v("my","error");
    e1.printStackTrace();
}

obj1=new JSONObject();
try {           
    obj1.put("who", facebookId);        
    obj1.put("visiblity", "public");
    obj1.put("poll", obj.toString());

} catch (JSONException e1) {
    //TODO Auto-generated catch block
    Log.v("my","error");
    e1.printStackTrace();
}
StringEntity se=null;
try {
    se = new StringEntity(obj1.toString());
} catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}  

try{
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
    httppost.setEntity(se);

    httppost.setHeader("Accept", "application/json");
    httppost.setHeader("Content-type", "application/json");
    Log.v("myapp", "works till here. 2");
    try {
        HttpResponse response = httpclient.execute(httppost);

        Log.v("myapp", "works till here. 3");

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
} catch (Exception e) {
    e.printStackTrace();
}

I want am getting a predefined JSON format in output.. { poll: '{"title":"dsfg","des_content_type":"text","poll_method":"list","options_type":"text","comments_enabled":"false","description":"Dfg "}', visiblity: 'public', who: '1758281246' }

Please somebody help me out.

2012-04-04 20:32
by Priyank Bharad


0

Your problem is here.

obj1=new JSONObject();           
obj1.put("who", facebookId);        
obj1.put("visiblity", "public");
obj1.put("poll", obj.toString());

Don't call obj.toString() when adding obj to obj1. Leave it as a JSONObject so that the classes can handle the internal formatting correctly.

2012-04-04 21:23
by Argyle
its done...thanx alooot.. - Priyank Bharad 2012-04-05 08:36
Ads