Relax Breath of Solution.Community tech blog of Sameera Thilakasiri - Consultant UI, UX, RWD Specialist/ Interactive Designer

Just I wanted.. Do you?…

HTTPService with Flex 3, example code – mx:HTTPService

Posted on February 13, 2010 | No Comments

A quick and short example of loading data (in this case from a XML file) using the wonderfully simple HTTPService in Flex 3. Thanks to data binding nearly all of the work is done for us. In this simple example I load a very simple XML file into an array and that is displayed in a datagrid with a data source binding to the array of loaded data.

Hopefully the formatting stays intact and no characters go missing from the code..

1. buy viagra pill We need to create the HTTPService tag after the application tag in the MXML file.

<mx:HTTPService id="dataSource" url="data/sampledata.xml"   result="resultHandler(event)"
fault="faultHandler(event)"/>

2. Next we add a creationComplete event to the Application tag (normally the second line of code in the MXML file).

creationComplete="dataSource.send()"

3. Time for the ActionScript, normally I do not place my code in the MXML file but to keep things simple here it is, just below the HTTPService tag.

<mx:Script>
	<![CDATA[
		import   mx.controls.Alert;
		import   mx.rpc.events.FaultEvent;
		import mx.rpc.events.ResultEvent;
		import     mx.collections.ArrayCollection;
 
		[Bindable]
		private var loadedData:ArrayCollection;
     
		private function resultHandler(event:ResultEvent):void
		{
			this.loadedData   = event.result.data.datanode;
		}
 
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show("Error:   " + event.fault.faultString, "Application Error");
		}
	]]/>;
</mx:Script>;

Of note here is the bindable variable ‘loadedData’ that the datagrid will be bound to. Also if an error should occur loading the data I have a popup appear warning us of the problem.

4. Finally we add a datagrid component to display the data.

<mx:DataGrid   x="10" y="10" dataProvider="{loadedData}" width="304"/>

You can see the source code here

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;   layout=&quot;absolute&quot;   width=&quot;324&quot;   height=&quot;243&quot;     creationComplete=&quot;dataSource.send()&quot;&gt;
&lt;mx:HTTPService id=&quot;dataSource&quot; url=&quot;data/sampledata.xml&quot;     result=&quot;resultHandler(event)&quot; fault=&quot;faultHandler(event)&quot;/&gt;
&lt;mx:Script&gt;
	&lt;![CDATA[
		import     mx.controls.Alert;
		import mx.rpc.events.FaultEvent;
		import     mx.rpc.events.ResultEvent;
		import <a href="http://jtc-enterprises.com/images/">buy levitra vardenafil</a>  mx.collections.ArrayCollection;
		
		[Bindable]
		private   var loadedData:ArrayCollection;
		
		private   function resultHandler(event:ResultEvent):void
		{
			this.loadedData = event.result.data.datanode;
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(&quot;Error: &quot; + event.fault.faultString, &quot;Application Error&quot;);
		}
	]]&gt;
&lt;/mx:Script&gt;
	&lt;mx:DataGrid       x=&quot;10&quot;   y=&quot;10&quot; dataProvider=&quot;{loadedData}&quot; width=&quot;304&quot;/&gt;
&lt;/mx:Application&gt;

Author
Sameera Thilakasiri By Sameera Thilakasiri
,is a front-end developer based in Colombo, is a blogger and a lifestyle photographer.
Follow him Twitter and Google+. Check out him.

Comments

Leave a Reply

You must be logged in to post a comment.