Flex與后臺交互的4種方法 HTTPService、URLLoader、WebService詳解
Flex與后臺交互的4種方法 HTTPService、URLLoader、WebService詳解?
HTTPService
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="<mx:Script>
<![CDATA[
private function initializeHandler(event:Event):void {
countriesService.send();
}
private function changeHandler(event:Event):void {
statesService.send();
}
]]>
</mx:Script>
<!-- 載入純靜態(tài)的xml數(shù)據(jù) -->
<mx:HTTPService id="countriesService" url="<!-- 載入由php生成的xml數(shù)據(jù) -->
<mx:HTTPService id="statesService" url="<!-- 以下標(biāo)簽就是要發(fā)送到服務(wù)端的數(shù)據(jù)了,可以這樣理解:有一個名為country的變量,它的值為花括號{}里的內(nèi)容 -->
<mx:request>
<country>{country.value}</country>
</mx:request>
</mx:HTTPService>
<mx:VBox>
<!-- 此控件的數(shù)據(jù)由第一個<mx:HTTPService/>控件接收的內(nèi)容提供,并且由這個ComboBox控制著第二個ComboBox所要顯示的內(nèi)容 -->
<mx:ComboBox id="country" dataProvider="{countriesService.lastResult.countries.country}"
change="changeHandler(event)" />
<!-- 下面的ComboBox已經(jīng)綁定了{statesService.lastResult.states.state},隨它的數(shù)據(jù)改變而改變 -->
<mx:ComboBox dataProvider="{statesService.lastResult.states.state}" />
</mx:VBox>
</mx:Application>
URLLoader
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="<mx:Script>
<![CDATA[
private var _countriesService:URLLoader;
private var _statesService:URLLoader;
private function initializeHandler(event:Event):void {
_countriesService = new URLLoader();
_countriesService.addEventListener(Event.COMPLETE, countriesCompleteHandler);
_countriesService.load(new URLRequest("_statesService = new URLLoader();
_statesService.addEventListener(Event.COMPLETE, statesCompleteHandler);
XML.ignoreWhitespace = true;
}
private function countriesCompleteHandler(event:Event):void {
var xml:XML = new XML(_countriesService.data);
country.dataProvider = xml.children();
}
private function statesCompleteHandler(event:Event):void {
var xml:XML = new XML(_statesService.data);
state.dataProvider = xml.children();
}
private function changeHandler(event:Event):void {
var request:URLRequest = new URLRequest("var parameters:URLVariables = new URLVariables();
parameters.country = country.value;
request.data = parameters;
_statesService.load(request);
}
]]>
</mx:Script>
<mx:VBox>
<mx:ComboBox id="country" change="changeHandler(event)" />
<mx:ComboBox id="state" />
</mx:VBox>
</mx:Application>
?
WebService 方法一
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="<mx:Script>
<![CDATA[
private function initializeHandler(event:Event):void {
statesService.getCountries();
}
private function changeHandler(event:Event):void {
statesService.getStates(country.value);
}
]]>
</mx:Script>
<mx:WebService id="statesService"
wsdl="<mx:operation name="getCountries" />
<mx:operation name="getStates" />
</mx:WebService>
<mx:VBox>
<mx:ComboBox id="country"
dataProvider="{statesService.getCountries.lastResult}" change="changeHandler(event)" />
<mx:ComboBox dataProvider="{statesService.getStates.lastResult}" />
</mx:VBox>
</mx:Application>
WebService 方法二
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="<mx:Script>
<![CDATA[
private function initializeHandler(event:Event):void {
statesService.getCountries.send( );
}
private function changeHandler(event:Event):void {
statesService.getStates.send( );
}
]]>
</mx:Script>
<mx:WebService id="statesService" wsdl="<mx:operation name="getCountries" />
<mx:operation name="getStates">
<mx:request>
<country>{country.value}</country>
</mx:request>
</mx:operation>
</mx:WebService>
<mx:VBox>
<mx:ComboBox id="country"
dataProvider="{statesService.getCountries.lastResult}" change="changeHandler(event)" />
<mx:ComboBox dataProvider="{statesService.getStates.lastResult}" />
</mx:VBox>
</mx:Application>
原文:http://bbs.airia.cn/FLEX/thread-2738-1-1.aspx
posted on 2009-12-31 14:48 飛熊 閱讀(563) 評論(0) 編輯 收藏 所屬分類: Flex