3

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?

2
  • Do you wanna publish the code (service attributes whcih defines post/get and the routing url) for .net web service? Not the whole implementation but where it defines URL routing and get/post. Commented Oct 27, 2010 at 10:55
  • @aliostad: this is my android client code.in myURL i specified my URL(10.242.48.54/restinsert/Service1.svc/CreateCustomer/Data). this service is implemented in .net which accepts only string as its input. Commented Oct 27, 2010 at 11:14

3 Answers 3

7

the solution is

HttpPost request = new HttpPost("http://10.242.48.54/restinsert/Service1.svc/save");
JSONStringer json = new JSONStringer()
.object() 
 .key("cname").value(name)
 .key("cmail").value(email)

.endObject();

StringEntity entity = new StringEntity(json.toString(), "UTF-8");
                     entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
                     entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
                     request.setEntity(entity); 
                     // Send request to WCF service 
                     DefaultHttpClient httpClient = new DefaultHttpClient();

                     HttpResponse response = httpClient.execute(request); 

This will send json object string, so in .net the request should have object parameter...

Sign up to request clarification or add additional context in comments.

Comments

2

For anyone having the same issue, here's a quick fix: - The webservice is expecting an object which includes a property of type String. In order to make this work, you must have a DataContract of the "expected" object.

For example, instead of asking for a string, create a class for your data:

[DataContract]
public class MyJSonString
{
 [DataMember]
    public String MyString {get;set;}
}

So, in your WCF method declaration you would have:

public void GetMyJsonString(MyJSonString mystring){...}

In you Java side, you have:

JSONStringer json = new JSONStringer().object().key("MyString").value("Hello!").endObject();

Comments

0

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

4 Comments

if i specify other than application/json,it is not accepting any values!
Are you sure that you've tried with specifying only the content-type header?
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
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: localtone.blogspot.com/2009/07/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.