【问题标题】:Android to .Net asmx web service communicationAndroid 与 .Net asmx 网络服务通信
【发布时间】:2013-10-28 17:56:15
【问题描述】:

我需要使用 web 服务 (.Net asmx),其中很少有方法接受 .Net 对象作为参数。例如,一种方法具有以下签名:

public Item SaveData(string itemName, string guid, Location storeAddress, Price price)

前 2 个参数是简单的对象(字符串),因此发送到 WS 没有什么特别的。我的问题是最后两个参数。该位置在 .Net 上定义为:

public class Location
{
    double longitude, latitude;

    public Location()
    {
    }

    public Location(double longitude, double latitude)
    {
        this.Longitude = longitude;
        this.Latitude = latitude;
    }

    public Location(String longitude, String latitude)
    {

        this.Longitude = double.Parse(longitude);
        this.Latitude = double.Parse(latitude);
    }

    public double Longitude
    {
        get { return longitude; }
        set { longitude = value; }
    }

    public double Latitude
    {
        get { return latitude; }
        set { latitude = value; }
    }
}

价格类似于 //不是所有代码

public class Price {
    //private vars

    public Price(float price, float discount, string currency){
        //vars assignment 
    }

//more methods and properties
}

现在在 Java (android) 上,我使用 HttpClient 和 HttpParams 来发送帖子数据。

我的问题是: 1.我如何构建JSON以将所需的参数发送到服务器? 2.有什么工具可以帮助我构建请求吗?我正在寻找能够阅读 wsdl 并构建示例请求的东西。

谢谢。

【问题讨论】:

  • 你需要storm 来检查你的wsdl
  • 风暴对我没有帮助。它不知道构建 JSON 请求。我不需要 XML。
  • 如果分析xml,构建json字符串就足够解释了。

标签: android web-services


【解决方案1】:

您可以通过分析您的网络方法的输入 XML 来构建 json 字符串。为此,我建议您使用免费工具,如 STORM(或功能丰富的付费工具,如 WCF Storm)来翻译您的 Web 服务的 WSDL。除此之外,您可以使用jsoneditoronline.orgjsonlint.com 来构建和检查json 字符串。

例如,以下是您需要传递给 web 方法的 json 字符串的结构。

{
    "itemName": "bla bla bla",
    "guid": "32sd4fgsd654fg53dfsg",
    "storeAddress": {
        "longitude": 12.02,
        "latitude": 10.32
    },
    "price": {
        "price": 100.01,
        "discount": 5.1,
        "currency": "USD"
    }
}

*您需要将虚拟值替换为真实值。

最后但同样重要的是,您可以使用一些 chrome 浏览器插件,例如 Postman - REST Client 将 json 字符串发送到您的网络服务并测试响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多