flex webservice 获取数据网络资源

<?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"
			    creationComplete="init()">
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			
			import mx.rpc.events.ResultEvent;
			
			import mx.rpc.soap.LoadEvent;
			import mx.rpc.soap.WebService;
			
			/* SOAP:简单对象访问协议,
			简单对象访问协议(SOAP)是一种轻量的、简单的、基于 XML 的协议,它被设计成在 WEB 上交换结构化的和固化的信息。
			SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议( HTTP),简单邮件传输协议(SMTP),
			多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。
			*/
			
			[Bindable]
			private var city:ArrayCollection =new ArrayCollection();
			
			private var web:WebService;//定义
			
			private function init():void
			{
				web=new WebService();
				web.wsdl="http://www.webservicex.net/globalweather.asmx?wsdl";//wsdl: Web Services Description Language
				
				web.loadWSDL();
				
				web.GetCitiesByCountry.addEventListener(ResultEvent.RESULT, OnGetCity);
				web.GetCitiesByCountry("china");
				//wsdl文件中
				/*
				<s:element name="GetCitiesByCountry">
					<s:complexType>
						<s:sequence>
							<s:element minOccurs="0" maxOccurs="1" name="CountryName" type="s:string"/>
						</s:sequence>
					</s:complexType>
				</s:element>
				<s:element name="GetCitiesByCountryResponse">
					<s:complexType>
						<s:sequence>
							<s:element minOccurs="0" maxOccurs="1" name="GetCitiesByCountryResult" type="s:string"/>
						</s:sequence>
					</s:complexType>
				</s:element>
				*/
				web.GetWeather.addEventListener(ResultEvent.RESULT,OnGetWeather);
				//wsdl文件中
				/*
				<s:element name="GetWeatherResponse">
					<s:complexType>
						<s:sequence>
							<s:element minOccurs="0" maxOccurs="1" name="GetWeatherResult" type="s:string"/>
						</s:sequence>
					</s:complexType>
				</s:element>
				*/
			}
			
			private function OnLoad(event:Event):void
			{
				pnlWeather.title+="------ webservice connected ... " 
			}
			
			private function OnGetCity(event:ResultEvent ):void
			{
				var xml:XML=new XML(event.result);
				for each ( var name:XML in xml..City ) {
					//city.push(name);
					city.addItem(name);
				}  
				cmbCity.selectedIndex=0;
			}
			
			private function OnGetWeather(event:ResultEvent ):void
			{
				txtRWeather.text="";
				var xml:XML=new XML(event.result);
				var xmlList:XMLList=xml.children();//???
				for (var i:int =0;i<xmlList.length();i++)
				{
					txtRWeather.text+= xmlList[i].toXMLString()+"\n"+"\n";
				}
			}
			
			private function onClick():void
			{
				web.GetWeather(cmbCity.textInput.text,"china");  
				//wsdl文件中的格式
				/* 
				<s:element name="GetWeather">
					<s:complexType>
						<s:sequence>
							<s:element minOccurs="0" maxOccurs="1" name="CityName" type="s:string"/>
							<s:element minOccurs="0" maxOccurs="1" name="CountryName" type="s:string"/>
						</s:sequence>
					</s:complexType>
				</s:element>
				*/
			}
		]]>
	</fx:Script>
	<s:Panel title="Weather" height="262" width="421" id="pnlWeather">
		<s:Label text="city:" x="74" y="24"/>
		<s:ComboBox x="130" y="24" width="106" id="cmbCity" dataProvider="{city}" />
		<s:Button x="258" y="24" label="submit" click="onClick()"/>
		<s:TextArea x="130" y="54" width="261" height="156" id="txtRWeather"/>
		<s:Label x="74" y="53" text="weather"/>	
	</s:Panel>
</s:Application>

  

  运行截图:

posted @ 2011-07-26 15:28  logzh  阅读(425)  评论(0编辑  收藏  举报