flex连接WebService简单例子

<?xml version="1.0" encoding="utf-8"?>

<!-- 

(C) aisajiajiao 2011

Flex调用WebService

-->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

	

	<mx:Script>

		<![CDATA[

			

			import mx.rpc.events.ResultEvent;

			import mx.rpc.events.FaultEvent;

			import mx.rpc.soap.LoadEvent;

			import mx.rpc.soap.WebService;

			import mx.controls.Alert;

			import mx.collections.ArrayCollection;

			

			//private var str:String = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";

			private var str:String = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl";

			private var webService:WebService;

			[Bindable]

			public var ss:ArrayCollection;

			private function onClick():void

			{

				//创建并实例化WebService示例,不过之前要先导入WebService类

				webService = new WebService();

				webService.wsdl = str;

				webService.loadWSDL(str);

				//事件侦听我就不说了,文档上都有

				webService.addEventListener(LoadEvent.LOAD, onLoad);

				webService.addEventListener(ResultEvent.RESULT, onResult);

				webService.addEventListener(FaultEvent.FAULT, onFault);

				//webService。GetWeatherByZipCode.addEventListener(ResultEvent.RESULT, onResult);

				//webService。GetWeatherByZipCode.addEventListener(FaultEvent.FAULT, onFault);

			}

			

			private function onLoad(e:LoadEvent):void

			{

				//进行相关的操作

				//webService.getWeatherbyCityName(txt.text.toString());

				webService.TranslatorString(txt.text.toString());

			}

			

			private function onResult(e:ResultEvent):void

			{

				ss=(e.result) as ArrayCollection;

				for(var i:int=0;i<ss.length;i++){

					myWeatherReport.text += ss[i]+"\n";

				}

				

			}

			

			private function onFault(e:FaultEvent):void

			{

				trace(e.message);

				Alert.show("错误:"+e.fault.faultString+"细节:"+e.fault.faultDetail,"错误");

			}

		]]>

	</mx:Script>

	

	<mx:Button label="点击开始转换" click="onClick()" />

	<mx:Text id="txt" text="hello" />

	<mx:TextArea id="myWeatherReport" height="100%" width="80%" editable="false" />

</mx:Application>

  要先看他的WSDL查看数据输入输出类型

你可能感兴趣的:(webservice)