我正在开发 Android 应用程序,但对我的 WCF Restful WS 的“POST”请求(像往常一样)有问题。
宁静的方法:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "createUser")]
string createUser(string user);
我为了找到我放在方法实现的注释体下的一个错误,现在它看起来像:
public string createUser(string user)
{
return user;
}
我已经用 JSON.stringify(datos) 从 JS(jQuery, Ajax) 调用这个方法数百次了,没有任何问题。现在我需要从 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>
请问有什么建议或帮助吗?