1

I 'm pretty new to Android. So, I 'm struggling with a HttpPost in android with Json Data.

JSONObject jsonObj = new JSONObject(); JSONObject jsonObjDasUser = new JSONObject(); jsonObj.put("name", "Login"); jsonObj.put("type", "request"); jsonObj.put("UserCredential", jsonObjUserCredential ); jsonObjUserCredential .put("username", id); jsonObjUserCredential .put("password", password); // Create the POST object and add the parameters HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpClient client = new DefaultHttpClient(); 

How can I include the jsonObjUserCredential as a entity like:::

//StringEntity userCredential = new StringEntity(jsonObjUserCredential .toString(),HTTP.UTF_8);?????? 

What am doing wrong here?????

Please Help me....

2
  • means you want to post Json data to we server right? what is jsonObjUserCredential can u plz post more code Commented Dec 3, 2012 at 16:58
  • jsonObjUserCredential has contained two Attributes(username and password) Commented Dec 4, 2012 at 11:32

2 Answers 2

5

Try this code .. Hope this fix ur problem..

JSONObject jsonObj = new JSONObject(); JSONObject jsonObjDasUser = new JSONObject(); jsonObj.put("name", "Login"); jsonObj.put("type", "request"); jsonObjUserCredential .put("username", id); jsonObjUserCredential .put("password", password); jsonObj.put("UserCredential", jsonObjUserCredential ); // Create the POST object and add the parameters HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8); 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your code.. For more detail My Json looks like this... { "name":"Login" ,"type":"request" ,"UserCredential": { "id": "xxxxx", "password": "xxxx" } } If I do the same way as you 've said..'d that be a good JsonBody to do the HttpPost? Thanks Dude
Thanks Sandeep .... I can see that the same JsonPost did posted but with change of order thats mean like this: {"UserCredential":{"password":"xxxxx","id":"xxxxx"},"type":"request","name":"Login"} That shouldn't be a issue is in it? Now I 've this issue.... default buffer size used in bufferedreader constructor it would be better tobe explicit if an 8k-char buffer is required How ca I fix it in my HttpPost ??
How can I query the JSON string in ASP.Net MVC server.?
Don't you have to add the stringentity to the post request?
1

try something like this:

List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("myJSON", MyJSONObject.toString())); //Set the http request httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); httpPost = new HttpPost(webServiceUrl + methodName); httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); //Variable to keep the http response response = null; //Set the parameters if exist if(params != null && !params.isEmpty()){ try { //Set the parameters in the request httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } //Execute the call response = httpClient.execute(httpPost,localContext); 

hope it helps

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.