Android post request (JSON parameter) to the WCF restful WS -
i working on android application , have problem "post" request (as usual) wcf restful ws.
restful method:
[operationcontract] [webinvoke(method = "post", requestformat = webmessageformat.json, uritemplate = "createuser")] string createuser(string user);
i order find error put under comment body of method implementation, , looks like:
public string createuser(string user) { return user; }
i have been calling method hundreds time js(jquery, ajax), json.stringify(datos), without problem. need same thing android application. snippet of code cause problem:
jsonobject user = new jsonobject(); user.put("id", "15"); user.put("name", "john"); user.put("city", "france"); user.put("email", "myemail"); user.put("password", "mypass"); httpclient client = new defaulthttpclient(); uri website = new uri("http://10.0.2.2/autooglasi/service1.svc/createuser"); httppost request = new httppost(); request.setentity(new stringentity(user.tostring())); request.addheader("content-type", "application/json"); request.seturi(website); httpresponse response = client.execute(request);
i getting response server (there no error on client side):
the exception message 'there error deserializing object of type system.string. end element 'root' namespace '' expected. found element 'id' namespace ''.'.see server logs more details. exception stack trace is: </p> <p>at system.servicemodel.dispatcher.operationformatter.deserializerequest(message message, object[] parameters) @ system.servicemodel.dispatcher.demultiplexingdispatchmessageformatter.deserializerequest(me ssage message, object[] parameters) @ system.servicemodel.dispatcher.uritemplatedispatchformatter.deserializerequest(message message, object[] parameters) @ system.servicemodel.dispatcher.dispatchoperationruntime.deserializeinputs(messagerpc& rpc) @ system.servicemodel.dispatcher.dispatchoperationruntime.invokebegin(messagerpc& rpc) @ system.servicemodel.dispatcher.immutabledispatchruntime.processmessage5(messagerpc& rpc) @ system.servicemodel.dispatcher.immutabledispatchruntime.processmessage31(messagerpc& rpc) @ system.servicemodel.dispatcher.messagerpc.process(boolean isoperationcontextset)</p> </div> </body></html>
any advice or help, please?
i have funnily solved problem:
1) had add bodystyle = webmessagebodystyle.wrappedrequest service method avoid error above. not understand why, because worked jquery ajax call without bodystyle parameter. appreciate if explain it?
2)i used: string s = "{\"id\":\"1\", \"name\":\"john\", \"user\":\"new yourk\", \"email\":\"mypass\", \"password\":\"myemail\"}"; test string. changed name of parameter "city" user. sent service , response got "new yourk", 1 parameter has name ws method parameter.
it means need send 1 parameter name "user" , value. like:
jsonobject uservalue = new jsonobject(); jsonobject user = new jsonobject(); uservalue .put("id", "15"); uservalue .put("name", "john"); uservalue .put("city", "france"); uservalue .put("email", "myemail"); uservalue .put("password", "mypass"); user.put("user", uservalue.tostring());
now request complete , server side can process user parameters.
note: important add "uservalue" string (user.put("user", uservalue.tostring())). if add json (user.put("user", uservalue)) not work.
Comments
Post a Comment