【发布时间】:2013-03-27 20:06:44
【问题描述】:
我正在开发 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;
}
我已经从 JS(jQuery, Ajax) 使用 JSON.stringify(datos) 调用此方法数百次,没有任何问题。 现在我需要从 android 应用程序做同样的事情。 这可能是我的代码的 sn-p 导致问题:
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>
有什么建议或帮助吗?
【问题讨论】: