Android post请求(JSON参数)到WCF restful WS

时间:2013-04-05 12:00:00

标签: android json wcf wcf-rest

我正在研究Android应用程序,我的问题与我的" POST"请求(像往常一样)到我的WCF Restful WS。

宁静的方法:

 [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应用程序做同样的事情。 这可能是导致问题的代码片段:

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>

有任何建议或帮助吗?

2 个答案:

答案 0 :(得分:2)

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

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

2)我使用过:String s =“{\”id \“:\”1 \“,\”name \“:\”John \“,\”user \“:\”New Yourk \“, \“email \”:\“mypass \”,\“password \”:\“myemail \”}“;作为测试字符串。我将参数“city”的名称更改为user。我发送它给我的服务,作为回复,我得到了“New Yourk”,只有一个参数,其名称为我的WS方法参数。

这意味着我需要发送一个名为“user”和value的参数。类似的东西:

 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)),它将无法工作。

答案 1 :(得分:0)

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

我猜应该是这样:

user.put(“id”,15);