[英]Passing JSON object from Android Client to WCF Restful service in C#
I have an android client which sends data in JSON object form to my server. 我有一个Android客户端,它以JSON对象形式将数据发送到我的服务器。 My server is implemented by WCF acting as a RESTful service written in C#.
我的服务器由WCF实现,充当用C#编写的RESTful服务。 I have a class called "User" in my WCF and I want to perform login action in android client.
我的WCF中有一个名为“User”的类,我想在android客户端中执行登录操作。 But when I post my object in JSON format to WCF service, I get a Null object (in Wrapped config) or I get an object which fields are null (in Bare config).
但是当我以JSON格式将对象发布到WCF服务时,我得到一个Null对象(在Wrapped配置中)或者我得到一个字段为空的对象(在Bare配置中)。 Does anyone have a solution for this?
有人有解决方案吗?
Here is a sample of generated JSON by my client which : 以下是我的客户生成的JSON示例:
{"User":{"UserName":"123","Pass":"123","Device":"123"}} { “用户”:{ “用户名”: “123”, “通”: “123”, “设备”: “123”}}
This is my WCF Interface code : 这是我的WCF接口代码:
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "Login",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json
)]
string Login(User user);
And this is my App.Config : 这是我的App.Config:
<system.serviceModel>
<services>
<service behaviorConfiguration="CityManService.TrackingBehavior"
name="CityManService.Tracking">
<endpoint address="" behaviorConfiguration="json" binding="webHttpBinding"
contract="CityManService.ITrackingService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/CityManService/Tracking/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="json">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="CityManService.TrackingBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
And Finally this is my client(android) code: 最后这是我的客户端(android)代码:
HttpPost request = new HttpPost("http://localhost:8731/CityManService/Tracking/Login");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
JSONStringer userJson = new JSONStringer()
.object()
.key("User")
.object()
.key("UserName").value(username.getText().toString()) .key("Pass").value(password.getText().toString()) .key("Device").value(password.getText().toString())
.endObject()
.endObject();
StringEntity entity = new StringEntity(userJson.toString(),"UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
entity.setContentType("application/json");
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
The name of the wrapper should be the parameter name, not the type name. 包装器的名称应该是参数名称,而不是类型名称。 In your service, the operation is defined as
在您的服务中,操作定义为
[WebInvoke(...)]
string Login(User user);
So the input should be passed as 所以输入应该作为传递
{"user":{"UserName":"123","Pass":"123","Device":"123"}}
(notice the lowercase "user" object name). (注意小写的“用户”对象名称)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.