【问题标题】:Android JSON post to WCF ServiceAndroid JSON 发布到 WCF 服务
【发布时间】:2012-10-16 20:02:52
【问题描述】:

我的 android 客户端尝试将 JSON 类发布到我的 wcf 服务时遇到问题。 这是android客户端的代码:

    public HttpResponse TestPost() throws Exception 
{

    HttpPost httpost = new HttpPost(url+"/TestPost");

    JSONStringer img = new JSONStringer()
        .object()
        .key("TestModel")
            .object()
                .key("p1").value("test")
                .key("p2").value("test")
                .key("p3").value(1)
                .key("p4").value("test")
                .key("p5").value(2)
                .key("p6").value("test;test")
            .endObject()
        .endObject();
        StringEntity se = new StringEntity(img.toString());

    httpost.setEntity(se);

    httpost.setHeader("Accept", "application/json");
    httpost.setHeader("Content-type", "application/json");

    return httpclient.execute(httpost);
}

这是 Wcf 的代码

    [OperationContract]
    [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "TestPost")]
    void TestPost(TestModel tm);





[DataContract]
public class TestModel
{
    [DataMember(Name = "p1")]
    public string p1 { get; set; }

    [DataMember(Name = "p2")]
    public string p2{ get; set; }

    [DataMember(Name = "p3")]
    public int p3 { get; set; }

    [DataMember(Name = "p4")]
    public string p4 { get; set; }

    [DataMember(Name = "p5")]
    public int p5 { get; set; }

    [DataMember(Name = "p6")]
    public string p6 { get; set; }

}

在我的 wcf 方法中,参数 TestModel tm 始终为空。可能有什么问题?

【问题讨论】:

  • 你检查过生成的 json 吗?
  • 是的,它正在生成。

标签: android json wcf post


【解决方案1】:

对象的包装(因为您指定了WebMessageBodyStyle.Wrapped)是基于参数name,而不是参数类型。最外层 JSON 成员的名称应该是“tm”,而不是“TestModel”:

public HttpResponse TestPost() throws Exception  
{ 
    HttpPost httpost = new HttpPost(url+"/TestPost"); 

    JSONStringer img = new JSONStringer() 
        .object() 
        .key("tm") 
            .object() 
                .key("p1").value("test") 
                .key("p2").value("test") 
                .key("p3").value(1) 
                .key("p4").value("test") 
                .key("p5").value(2) 
                .key("p6").value("test;test") 
            .endObject() 
        .endObject(); 
        StringEntity se = new StringEntity(img.toString()); 

    httpost.setEntity(se); 

    httpost.setHeader("Accept", "application/json"); 
    httpost.setHeader("Content-type", "application/json"); 

    return httpclient.execute(httpost); 
} 

【讨论】:

  • 谢谢,错过了:)。我还需要将内容类型和内容编码设置为 UTF-8 才能让 i 工作。 StringEntity 实体 = 新 StringEntity(json.toString()); entity.setContentType("application/json;charset=UTF-8"); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8")); httpost.setEntity(entity);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多