Android JSON发布到WCF服务 [英] Android JSON post to WCF Service

查看:103
本文介绍了Android JSON发布到WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I having a problem with my android client trying to post a JSON class to my wcf service. Here is the code for android client :

    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始终为null.可能有什么问题吗?

In my wcf method the parameter TestModel tm is always null. What could possibly be wrong?

推荐答案

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

The wrapping of the object (since you specified WebMessageBodyStyle.Wrapped) is done based on the parameter name, not the parameter type. The name of the outermost JSON member should be "tm", not "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); 
} 

这篇关于Android JSON发布到WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆