1

我正在开发 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&amp;      rpc) at
 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc) at
 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc) at
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
 at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p> </div> </body></html>

请问有什么建议或帮助吗?

4

2 回答 2

2

我有趣地解决了这个问题:

1)我必须将 BodyStyle = WebMessageBodyStyle.WrappedRequest 添加到我的服务方法中以避免上述错误。我真的不明白为什么,因为它与没有 BodyStyle 参数的 jQuery Ajax 调用完美配合。如果有人能解释一下,我将不胜感激?

2)我使用:String s = "{\"id\":\"1\", \"name\":\"John\", \"user\":\"New Yourk\", \"email \":\"我的密码\", \"密码\":\"我的邮箱\"}"; 作为测试字符串。我将参数“城市”的名称更改为用户。我将它发送到我的服务,作为响应,我得到了“New Yourk”,只有一个参数名称为我的 WS 方法参数。

这意味着我需要发送一个名为“用户”和值的参数。就像是:

 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());

现在我的请求已经完成,我的服务器端可以处理我的用户参数了。

注意:将“userValue”添加为字符串(user.put("user", userValue.toString()))非常重要。如果我们将它添加为 Json (user.put("user", userValue)) 它将不起作用。

于 2013-04-10T11:16:57.263 回答
0

不知道它是否与这个问题有关,但是id应该是一个字符串吗?

我猜应该是这样的:

user.put("id", 15);

于 2013-04-05T12:20:22.757 回答