Flex4与WebService通信
首先需要一个需要提供一个WebService服务。 这里用一个天气预报的webservice为例,如中国气象局的免费服务http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
通过下面这个网址可以输入城市,查询城市天气预报
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName
下面开始示例:
xml代码:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<!--[CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
//调用成功
protected function webservice_resultHandler(event:ResultEvent):void
{
message.text = (String)(event.result);
}
//调用失败
protected function webservice_faultHandler(event:FaultEvent):void
{
Alert.show("调用失败了," + event.fault.message as String, "提示");
}
protected function serach_clickHandler(event:MouseEvent):void
{
webservice.getWeatherbyCityName(city.text);
}
]]-->
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:WebService id="webservice"
wsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"
result="webservice_resultHandler(event)"
fault="webservice_faultHandler(event)"
showBusyCursor="true" />
</fx:Declarations>
<s:TextInput x="138" y="85" id="city"/>
<s:Button x="298" y="86" label="Search" id="serach" click="serach_clickHandler(event)" />
<s:TextArea x="138" y="136" width="441" id="message"/>
</s:Application>
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<!--[CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
//调用成功
protected function webservice_resultHandler(event:ResultEvent):void
{
message.text = (String)(event.result);
}
//调用失败
protected function webservice_faultHandler(event:FaultEvent):void
{
Alert.show("调用失败了," + event.fault.message as String, "提示");
}
protected function serach_clickHandler(event:MouseEvent):void
{
webservice.getWeatherbyCityName(city.text);
}
]]-->
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:WebService id="webservice"
wsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"
result="webservice_resultHandler(event)"
fault="webservice_faultHandler(event)"
showBusyCursor="true" />
</fx:Declarations>
<s:TextInput x="138" y="85" id="city"/>
<s:Button x="298" y="86" label="Search" id="serach" click="serach_clickHandler(event)" />
<s:TextArea x="138" y="136" width="441" id="message"/>
</s:Application>
OK,运行应用程序,输入城市名称,you can see: