将Android类发送到WCF Web服务
本文关键字:WCF Web 服务 Android | 更新日期: 2023-09-27 18:25:47
我正在尝试使用REST,将其发送到公开的Web服务。但我得到的是400(错误请求)
如何从REST发送一个模型,它将正确映射到另一端的模型。我不想做的是在连接点中单独暴露所有100个参数。
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "SaveData",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
void SaveData(datum data);
但我不能做的是传递一个有效的基准对象。
// Build JSON string
JSONStringer vehicle;
try {
vehicle = new JSONStringer()
.object()
.key("datum")
.object()
.key("ID").value(5)
.key("Name").value("test")
.key("No").value(54)
.key("Description").value("Test")
.endObject()
.endObject();
HttpPost request = new HttpPost(address + "/SaveData");
Log.d("WebInvoke", "Connection : " + address + "/SaveData");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
StringEntity entity = new StringEntity(vehicle.toString());
Log.d("StringEntity", vehicle.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
// Saving : 400
基准等级:
[DataContract]
public partial class datum
{
[DataMember(Name = "ID")]
public int ID { get; set; }
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "No")]
public Nullable<int> No { get; set; }
[DataMember(Name = "Description")]
public string Description { get; set; }
}
应该是:
vehicle = new JSONStringer()
.object()
.key("data")
.object()
.key("ID").value(5)
.key("Name").value("test")
.key("No").value(54)
.key("Description").value("Test")
.endObject()
.endObject();
来源:WCF BodyStyle WrappedRequest没有';是否适用于传入的JSON参数?