2013-04-05 79 views
1

我正在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&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

我都好笑解决了这个问题:我不得不BodyStyle = WebMessageBodyStyle.WrappedRequest添加到我的服务方法,以避免上述错误

1)。我不明白为什么,因为它完全适用于没有BodyStyle参数的jQuery Ajax调用。如果有人能解释它,我将不胜感激?我使用:String s =“{\”id \“:\”1 \“,\”name \“:\”John \“,\”user \“:\”New Yourk \“ ,\“email \”:\“mypass \”,\“密码\”:\“myemail \”}“;作为测试字符串。我更改了参数“城市”的名称给用户。我将它发送给我的服务,并作为响应我得到了“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)),它将不起作用。

0

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

我猜应该是这个:

user.put( “ID”,15);

+0

对不起,我刚刚编辑了我的最后一句话,我没有收到错误,这是我从服务器的回应。我试过你的建议,没有什么改变。 – Milos 2013-04-05 12:41:52

+0

我在服务器端的参数是一个字符串(JSON),我应该解析,没有任何其他类型的字符串除外。 – Milos 2013-04-05 12:48:33