tags:

views:

56

answers:

1

hi,

Json object is returned to a webservice which gets the i/p as

[OperationContract(Name = "Create")]
[WebInvoke(UriTemplate = "/Create/Data", Method = "POST",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat=WebMessageFormat.Json)]

bool CreateCustomer(StringBuilder objCustomer);

//in my service file im deserializing the i/p obj to my class object and inserting

public bool Create(StringBuilder strObj)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();

        Customer custObj = js.Deserialize<Customer>(strObj.ToString());

        bool Inserted = false;

// connection established and data is put into it

from im asp.net client the obj is serialized to json format and it can be inserted into my DB but from android, the JSON object which they are sending could not be recognised by my service.. but the response to them goes as "OK 200" ..

the android code is

HttpClient client = new DefaultHttpClient(); 
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 

HttpResponse response; 
JSONObject json = new JSONObject();
String URL ="http://10.242.48.54/restinsert/Service1.svc/Create/Data";
try{ 

    HttpPost post = new HttpPost(URL); 
    json.put("CNo",200);
    json.put("CName","addme");

    StringEntity se = new StringEntity(json.toString());
    se.setContentType("application/json; charset=utf-8");
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8"));

    post.setHeader("Accept","application/json");
    post.setHeader("Content-type","application/json; charset=utf-8");
    String ss= post.toString();

    response = client.execute(post);

created a new class for serialize and deserialize

public class Json
{
    public string JsontoString(string obj)
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        StringBuilder sb = new StringBuilder();
        js.Serialize(obj, sb);
        return sb.ToString();
    }

    public string StringtoJson(string obj)
    {

        JavaScriptSerializer js = new JavaScriptSerializer();
        Customer custObj = js.Deserialize<Customer>(obj.ToString());
        return custObj.ToString();
    }
}

}

A: 

Having never used the Json Serializer, but having a slight clue about how WCF does its deserialization I would say you can't have StringBuilder as a parameter.

Just change your operation signature to:

bool CreateCustomer(Customer customer);

WCF wants to do the serialization and deserialization itself, so let it.

Darrel Miller
thanks darrel ..! will work on it further
ganesh
but is it possible for the android client to send object as customer ?? can they deserialze the json to customer obj and send to .net wcf REST??
ganesh