我正在Android应用程序上工作,我有我的“POST”请求(像往常一样)到我的WCF Restful WS的问题。Android发布请求(JSON参数)到WCF restful WS
RESTful方法:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "createUser")]
string createUser(string user);
我为了找错误我的方法实现的评论身下放的,现在它看起来像:
public string createUser(string user)
{
return user;
}
我一直在呼吁这个方法数百次来自JS(jQuery,Ajax)和JSON.stringify(数据),没有任何问题。 现在我需要从android应用程序做同样的事情。 这可能是片断的我的代码引起的问题:
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);
我正在从服务器的响应(没有错误在我的客户端):
The exception message is 'There was an error deserializing the object of type
System.String. End element 'root' from namespace '' expected. Found element 'id' from
namespace ''.'.See server logs for more details. The exception stack trace is: </p> <p>at
System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message,
Object[] parameters) at
System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Me ssage message, Object[] parameters) at
System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at
System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p> </div> </body></html>
任何建议或帮助,请?
对不起,我刚刚编辑了我的最后一句话,我没有收到错误,这是我从服务器的回应。我试过你的建议,没有什么改变。 – Milos 2013-04-05 12:41:52
我在服务器端的参数是一个字符串(JSON),我应该解析,没有任何其他类型的字符串除外。 – Milos 2013-04-05 12:48:33