Flex using the WebService class

Category: Flex Examples, Learn Flex, flex tutorials    |    386 views

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

Share/Save/Bookmark

1 Star2 Stars

Tags: , , ,

1 response so far!

Leave a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word