SOAP调用不适用于Titanium

时间:2015-09-11 06:39:34

标签: xml api soap titanium

有人请解释为什么它不起作用?

//Create our application namespace
var my = {
suds : require('suds2'),
isAndroid : (Ti.Platform.osname === 'android'),
config : {
    endpoint:"http://www.webservicex.net/whois.asmx",
    targetNamespace: 'http://www.webserviceX.NET/',
    includeNS : true, 
    ns: 'ns0', 
    addTargetSchema : false                         
}   
};


// Create a simple window to show our results
(function(){

var win = Ti.UI.createWindow({
    backgroundColor:'#fff', layout:'vertical'
});

win.add(Ti.UI.createLabel({
    text:"SOAP Service", color:'#000', top:5, height:22, font:{fontWeight:'bold',fontSize:16} 
}));
win.add(Ti.UI.createLabel({
    text:"Press Go to see result", color:'#000', top:5, height:22, font:{fontSize:14} 
}));    

var label = Ti.UI.createLabel({
    top: 10, left: 10,
    width: 'auto', height: 'auto',
    text: 'Press go to start'
});
win.add(label);

var goButton = Ti.UI.createButton({
    title:'Go', left:10, top:10
});
win.add(goButton);

goButton.addEventListener('click',function(e){

    label.text ="Loading...";

    try {

        var sudsClient = new my.suds(my.config);

        sudsClient.invoke('GetWhoIS', 
            {
                HostName : 'www.google.com'
            }, 
            function(xmlDoc) {
                var results = xmlDoc.documentElement.getElementsByTagName('GetWhoISResponse');
                if (results.length>0) {
                    label.text = results.item(0).text;
                } else {
                    label.text = 'Oops, could not determine result of SOAP call.';
                }
            });

    } catch(e) {
        Ti.API.error('Error: ' + e);
    }       
});

win.open(); 

})();

注意:它正在为此服务工作:http://www.webservicex.net/CurrencyConvertor.asmx 调用函数:ConversionRate

参数: FromCurrency:'EUR', ToCurrency:'USD'

回调功能: ConversionRateResult

2 个答案:

答案 0 :(得分:0)

这是实现SOAP通信的旧方法,ti-soap是最好的,从Node.js SOAP移植到Titanium。

此外,如果您使用Alloy合集,则可以使用我的Alloy SOAP Adapter,这样可以更轻松地处理连接,自动绑定到视图。

答案 1 :(得分:0)

var soapRequest = "XML XML XML"; //write your xml request including the   parameters

var client = Ti.Network.createHTTPClient();

client.onerror = function(e) {Ti.API.info(e);};

client.onload = function(e){var doc = this.responseXML.documentElement;}; //the response, you'll have to manually parse the xml

client.open('POST', 'url'); //endpoint

client.setRequestHeader("SOAPAction", "method"); //method of the web service.

client.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

client.send(soapRequest); //send the xml