方式一:flex与Webservices与简单通信
1、格式:
flex中有个<webservices>的标签能够负责flex与webservices之间交互
view plaincopy to clipboardprint?
<mx:WebService id=" WebService组件id" wsdl="WebService地址">
<mx:operation name="方法名">
<mx:request>
<参数名1>值1</参数名1>
<参数名2>值2</参数名2> …
</mx:request>
</mx:operation>
</mx:WebService>
<mx:WebService id=" WebService组件id" wsdl="WebService地址">
<mx:operation name="方法名">
<mx:request>
<参数名1>值1</参数名1>
<参数名2>值2</参数名2> …
</mx:request>
</mx:operation>
</mx:WebService>
<mx:operation>组件表示WebService的一个方法,属性name值必须与WebService中的定义名称相同 <mx:request>标签下传递参数,需要注意的是参数的名称及顺序必须与WebService中的定义相同。
2、范例:
本实例是整理的网络上《五分钟用Flex构建WebService应用》的博客,使用的是“http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl”地址提供的天气服务数据
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:WebService id="webService"
wsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"
showBusyCursor="true"
result="onLoad(event)"
fault="faultHandler(event)"/>
<mx:Script>
<!--[CDATA[
import mx.collections.ArrayCollection;
import mx.utils.ArrayUtil;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
private function onLoad(event:ResultEvent):void
{
var weatherInfo:ArrayCollection=(event.result) as ArrayCollection;
for (var i:int=0; i < weatherInfo.length; i++)
{
myWeatherReport.text+=weatherInfo[i] + "/n";
}
}
private function faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.toString(), "WebService Error");
}
]]-->
</mx:Script>
<mx:TextInput id="cityname"
text="北京" x="10" y="38"/>
<mx:Button label="Call WebService"
click="webService.getWeatherbyCityName(cityname.text)" x="178" y="38"/>
<mx:TextArea id="myWeatherReport"
height="334"
width="409"
editable="false" x="10" y="82"/>
</mx:Application>
方式二:这里主要介绍Webservices使用AS代码的操作方法
As代码部分
view plaincopy to clipboardprint?
[Bindable]
public var arrayCol:ArrayCollection;
public function Test():void
{
var webs:WebService=new WebService();
webs.wsdl="http://localhost/Web/WebService1.asmx?WSDL";
webs.loadWSDL();
webs.showBusyCursor=true;
// webs.addEventListener(LoadEvent.LOAD,load);
webs.addEventListener(ResultEvent.RESULT,WebsResult);
webs.addEventListener(FaultEvent.FAULT,webFault);
webs.GetProData();
}
public function webFault(event:FaultEvent):void
{
Alert.show(event.toString());
}
public function WebsResult(Result:ResultEvent):void
{
arrayCol =Result.result as ArrayCollection;
}
[Bindable]
public var arrayCol:ArrayCollection;
public function Test():void
{
var webs:WebService=new WebService();
webs.wsdl="http://localhost/Web/WebService1.asmx?WSDL";
webs.loadWSDL();
webs.showBusyCursor=true;
// webs.addEventListener(LoadEvent.LOAD,load);
webs.addEventListener(ResultEvent.RESULT,WebsResult);
webs.addEventListener(FaultEvent.FAULT,webFault);
webs.GetProData();
}
public function webFault(event:FaultEvent):void
{
Alert.show(event.toString());
}
public function WebsResult(Result:ResultEvent):void
{
arrayCol =Result.result as ArrayCollection;
}
view plaincopy to clipboardprint?
<mx:AdvancedDataGrid id="adg1"
designViewDataType="tree" editable="true"
dataProvider="{arrayCol}">
<mx:columns>
<mx:AdvancedDataGridColumn headerText="CustomerID"
dataField="CustomerID"/>
<mx:AdvancedDataGridColumn headerText="ShipName"
editable="false"
dataField="ShipName"/>
<mx:AdvancedDataGridColumn headerText="Freight"
itemEditor="mx.controls.NumericStepper"
dataField="Freight"/>
</mx:columns>
</mx:AdvancedDataGrid>
<mx:Button label="testWeb" click="Test()"></mx:Button>