Search

Custom Search
03
Dec
08

Flex using the WebService class

Flex Builder 3 has taken it one step further with the introspection wizard. Using a modified version of Apache Axis framework, the new wizard generates proxy classes that takes care of Web Service data transfer and event plumbing.

The following example shows how you can call a ColdFusion Web Service from Flex using the <mx:WebService /> tag.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">


    <mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.utils.ObjectUtil;

private var startTime:int;
private var endTime:int;

private function button_click():void {
webService.getMonths.send();
startTime = getTimer();
lbl.text = "";
}

private function getMonths_result(evt:ResultEvent):void {
textArea.text = ObjectUtil.toString(evt.result);
calcTime();
}

private function getMonths_fault(evt:FaultEvent):void {
Alert.show(evt.type);
calcTime();
}

private function calcTime():void {
endTime = getTimer();
lbl.text = "total time: " + (endTime - startTime) + "ms";
}
]]>
</mx:Script>

<mx:WebService id="webService"
wsdl="http://www.flash-mx.com/ws/months.cfc?wsdl">
<mx:operation name="getMonths"
resultFormat="object"
result="getMonths_result(event);"
fault="getMonths_fault(event);" />
</mx:WebService>

<mx:ApplicationControlBar dock="true">
<mx:Button id="button"
label="get months from web service"
click="button_click();" />
<mx:Spacer width="100%" />
<mx:Label id="lbl" />
</mx:ApplicationControlBar>

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

</mx:Application>

If you’re using ColdFusion, the months web service is as follows:

<cfcomponent output="false">

<cffunction name="getMonths" access="remote" returntype="array" output="false">
<cfset var monthNames = ListToArray("January,February,March,April,May,June,July,August,September,October,November,December") />
<cfreturn monthNames />
</cffunction>

</cfcomponent>

Source

Sharing is loving

Get Adobe Flash playerPlugin by wpburn.com wordpress themes