views:

63

answers:

2

hi, i am trying to send a json string from my android client to my .net Rest service... can anyone help me in this issue?

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://myURL");

 JSONObject json = new JSONObject();
 json.put("name", "i am sample");
 StringEntity str = new StringEntity(json.toString());
 str.setContentType("application/json; charset=utf-8");
 str.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json; charset=utf-8"));
 post.setEntity(str);
 HttpResponse response = client.execute(post);

Response is bad request. am i sending the json object as string? is this code correct?

A: 

Looks valid except the following header:

str.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json; charset=utf-8"));

Content-encodings are described here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5

jgauffin
if i specify other than application/json,it is not accepting any values!
Adhavan
Are you sure that you've tried with specifying only the content-type header?
jgauffin
yes! In post i specified only content-type header, is there any thing that i have to add in that other then this? if so please let me know
Adhavan
Try with putting the content-type on the `post` variable instead. I think that putting the specifications on a `entity` will produce a multipart post. I found an example on the net: http://localtone.blogspot.com/2009/07/post-json-using-android-and-httpclient.html
jgauffin
yes i have seen this link but even that doesn't work.
Adhavan