简体   繁体   English

Android与.Net ASMX Web服务通信

[英]Android to .Net asmx web service communication

I need to use an webservice (.Net asmx) where are few methods accepting .Net objects as parameters. 我需要使用一种Web服务(.Net asmx),其中很少有接受.Net对象作为参数的方法。 One method, for example, has this signature: 例如,一种方法具有以下签名:

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

The first 2 parameters are simple objects (strings) so nothing special to send to the WS. 前两个参数是简单的对象(字符串),因此没有什么特别的要发送给WS的。 My issue is with the last 2 parameters. 我的问题是最后两个参数。 The location is defined on .Net as: 该位置在.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; }
    }
}

and the price something like //not all the code 和价格类似//不是所有代码

public class Price {
    //private vars

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

//more methods and properties
}

Now on Java (android), I'm using HttpClient with HttpParams to send post data. 现在在Java(android)上,我将HttpClient与HttpParams一起使用来发送帖子数据。

My questions are: 1. how do I build the JSON to send the required parameters to the server? 我的问题是:1.如何构建JSON以将所需参数发送到服务器? 2. is any tool to help me to build the request? 2.有什么工具可以帮助我建立请求? I'm looking for something that knows to read the wsdl and to build a sample request. 我正在寻找可以读取wsdl并建立示例请求的东西。

Thank you. 谢谢。

You can build json string by analyzing the input XML for your web-method. 您可以通过分析Web方法的输入XML来构建json字符串。 For that I would recommend you to use free tools like STORM (or feature-rich paid tools like WCF Storm ) that translates your web-service's WSDL . 为此,我建议您使用STORM之类的免费工具(或WCF Storm之类的功能丰富的付费工具)来翻译Web服务的WSDL Other than that, you may use jsoneditoronline.org and jsonlint.com to build and inspect the json string. 除此之外,您可以使用jsoneditoronline.orgjsonlint.com来构建和检查json字符串。

As an example, following is the structure for your json string that you need to pass to the web-method. 例如,以下是您需要传递给Web方法的json字符串的结构。

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

* You need to replace the dummy values with the real ones. * 您需要用真实值替换虚拟值。

Last but not least, you may use some chrome-browser plugins like Postman - REST Client to send the json string to your webservice and test the responses. 最后但并非最不重要的一点是,您可以使用一些Chrome浏览器插件(例如Postman-REST Client)将json字符串发送到您的网络服务并测试响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM