<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flex Tutorial &#187; Consume Web Services</title>
	<atom:link href="http://flextutorial.org/tag/consume-web-services/feed/" rel="self" type="application/rss+xml" />
	<link>http://flextutorial.org</link>
	<description>Rich Internet Application Development by Adobe Flex</description>
	<lastBuildDate>Fri, 11 Dec 2009 05:54:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Creating a Web Service Powered Flex Application in One Minute</title>
		<link>http://flextutorial.org/2009/03/04/creating-a-web-service-powered-flex-application-in-one-minute/</link>
		<comments>http://flextutorial.org/2009/03/04/creating-a-web-service-powered-flex-application-in-one-minute/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 19:56:08 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Flex and Web Services]]></category>
		<category><![CDATA[Consume Web Services]]></category>
		<category><![CDATA[Flex WebService]]></category>

		<guid isPermaLink="false">http://flextutorial.org/?p=163</guid>
		<description><![CDATA[This post belongs to the ‘How to Consume Web Services in Adobe Flex‘ series which provides tutorials and tips for Flex developers who want to access server-side data though Flex WebService. Read the introduction page here. Your Key to the World of Web Services &#8211; Flex WebService In old days, it sounded like very complex [...]]]></description>
			<content:encoded><![CDATA[<p><em>This post belongs to the ‘<a href="http://flextutorial.org/2009/03/03/how-to-consume-web-services-in-adobe-flex/">How to Consume Web Services in Adobe Flex</a>‘ series which provides tutorials and tips for Flex developers who want to access server-side data though Flex WebService. <a href="http://flextutorial.org/2009/03/03/how-to-consume-web-services-in-adobe-flex/">Read the introduction page here</a>.</em></p>
<p><strong><span style="color: #ff6600;">Your Key to the World of Web Services &#8211; </span></strong><strong><span style="color: #ff6600;">Flex WebService</span></strong></p>
<p><strong><span style="color: #ff6600;"><span id="more-163"></span></span></strong></p>
<p>In old days, it sounded like very complex to call web services. <em>SOAP, WSDL, Binding Style, Service, Port, Operation, Message, Type</em>&#8230; just name a few of technical jargons here, you might feel lost already.</p>
<p>However, it&#8217;s actually very easy  to consume web services in Adobe Flex because it provides a built-in component &#8211; <strong>WebService</strong>. By using it, you can literally <strong>Create a Web Service Powered Flex Application in One Minute</strong>. Is that amazing?</p>
<p><strong><span style="color: #ff6600;">Your First Flex WebService Application</span></strong></p>
<p>What we want to build here is a very simple Flex application, which converts a temperature value from Fahrenheit to Celsius. Behind the scene, it calls an online web service provided by <a href="http://www.w3schools.com/webservices/tempconvert.asmx">w3schools.com</a> to do the conversion.</p>
<p>The final Flex application will look like this:</p>
<p><img class="size-full wp-image-199 alignnone" title="flextutorial-003" src="http://flextutorial.org/wp-content/uploads/2009/03/flextutorial-003.jpg" alt="flextutorial-first-flex-webserivce-application-1" width="398" height="151" /></p>
<p>Once you type a Fahrenheit value (e.g. 100) into the input box and click <strong>Convert </strong>button, it will show the corresponding Celsius value.</p>
<p><img class="alignnone size-full wp-image-205" title="flextutorial-004" src="http://flextutorial.org/wp-content/uploads/2009/03/flextutorial-004.jpg" alt="flextutorial-first-flex-webserivce-application-2" width="398" height="151" /></p>
<p>Now, Let me show you how to build this Flex WebService application in <strong>4 simple steps</strong>. You can literally finish it within one minute.</p>
<p><strong><span style="color: #ff6600;">Step 1 &#8211; Create a New Project in Flex Builder</span></strong></p>
<ol>
<li><span style="color: #ff6600;"><span style="color: #000000;">Start your Flex Builder. On the top menu, click <strong>File -&gt; New -&gt; Flex Project</strong></span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">Give a name to your project, e.g. <em><strong>FlexWebService</strong></em>.</span></span><strong><span style="color: #ff6600;"> </span></strong></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">Keep the default value for the rest parts, click <strong>Finish </strong>button.</span><br />
</span></li>
</ol>
<p style="padding-left: 30px;"><strong><img class="size-full wp-image-190 alignnone" title="flextutorial-001" src="http://flextutorial.org/wp-content/uploads/2009/03/flextutorial-001.jpg" alt="flextutorial-new-flex-webservice-project" width="557" height="589" /></strong></p>
<p><strong><span style="color: #ff6600;">Step 2 &#8211; Add Flex WebService Component</span></strong></p>
<p><span style="color: #ff6600;"><span style="color: #000000;">Flex provides a MXML tag <strong>&lt;mx:WebService&gt;</strong> to call web services, that is all you need right now. So just add the following lines to you main MXML file.</span></span><strong><span style="color: #ff6600;"><br />
</span></strong></p>
<blockquote>
<pre><span style="color: #0000ff;">&lt;mx:WebService</span> id="<span style="color: #ff0000;">myWebService</span>"
    wsdl="<span style="color: #ff0000;">http://www.w3schools.com/webservices/tempconvert.asmx?WSDL</span>"&gt;
    <span style="color: #0000ff;">&lt;mx:operation</span> name="<span style="color: #ff0000;">FahrenheitToCelsius</span>"
        result="<span style="color: #ff0000;">resultHandler(event)</span>"
        fault="<span style="color: #ff0000;">faultHandler(event)</span>"<span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/mx:operation&gt;</span>
<span style="color: #0000ff;">&lt;/mx:WebService&gt;</span></pre>
</blockquote>
<p>What have we just done?</p>
<ul>
<li>We create a &lt;mx:WebService&gt; with id <span style="color: #ff0000;">myWebService</span><em>.</em></li>
<li>We point &lt;mx:WebService&gt; to the URL (<span style="color: #ff0000;">http://www.w3schools.com/webservices/tempconvert.asmx?WSDL) </span>of a wsdl file created by our web service provider.</li>
<li>We specify the operation to use (<span style="color: #ff0000;">FahrenheitToCelsius</span>) and the functions to handle the result and fault event (<span style="color: #ff0000;">resultHandler </span>and <span style="color: #ff0000;">faultHandler</span>).</li>
</ul>
<p><strong><span style="color: #ff6600;">Step 3 &#8211; Add Flex WebService Event Handlers<br />
</span></strong></p>
<p>Now, we add the following code to handle the Flex WebService result and fault event.</p>
<blockquote>
<pre><span style="color: #000000;"><span style="color: #339966;">&lt;mx:Script&gt;</span>
    &lt;![CDATA[
	<span style="color: #0000ff;">import</span> mx.rpc.events.FaultEvent;
	<span style="color: #0000ff;">import </span>mx.rpc.events.ResultEvent;
	<span style="color: #0000ff;">import </span>mx.controls.Alert;

	<span style="color: #0000ff;">private </span><span style="color: #339966;">function </span>resultHandler(event:ResultEvent):<span style="color: #0000ff;">void </span>{
		<span style="color: #ff0000;">cValue.text = event.result.toString();</span>
	}

	<span style="color: #0000ff;">private </span><span style="color: #339966;">function </span>faultHandler(event:FaultEvent):<span style="color: #0000ff;">void </span>{
		<span style="color: #ff0000;">Alert.show(event.fault.message, "Error");</span>
	}
    ]]&gt;
<span style="color: #339966;">&lt;/mx:Script&gt;</span></span></pre>
</blockquote>
<p>The above code should be very simple to follow:</p>
<ul>
<li><strong>resultHandler </strong>function gets the web service&#8217;s return data from ResultEvent (event.result), converts it to a string, then bind it to a Flex Text component (cValue) for display.</li>
<li><strong>faultHandler </strong>function gets the fault error message from FaultEvent (event.fault.message) and use Alert window to display it.</li>
</ul>
<p><strong><span style="color: #ff6600;">Step 4 &#8211; Add Flex UI Components to Display the Temperature Converter<br />
</span></strong></p>
<p>In this last step, let&#8217;s create a simple UI:</p>
<blockquote>
<pre><span style="color: #000000;"><span style="color: #000000;"><span style="color: #0000ff;">&lt;mx:Panel</span> title="Fahrenheit to Celsis Converter"
    paddingTop="30" paddingRight="10"
    paddingBottom="30" paddingLeft="10"<span style="color: #0000ff;">&gt;</span>

    <span style="color: #0000ff;">&lt;mx:HBox&gt;</span>
	<span style="color: #0000ff;">&lt;mx:Label</span> text="Fahrenheit Value: "<span style="color: #0000ff;">/&gt;</span>
	<span style="color: #0000ff;">&lt;mx:TextInput</span> id="fValue" <span style="color: #0000ff;">/&gt;</span>
	<span style="color: #0000ff;">&lt;mx:Button</span> id="submit" label="Convert"
           click="<span style="color: #ff0000;">myWebService.FahrenheitToCelsius.send(fValue.text)</span>"<span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/mx:HBox&gt;</span>

    <span style="color: #0000ff;">&lt;mx:HBox&gt;</span>
	<span style="color: #0000ff;">&lt;mx:Label</span> text="Celsius Value: "<span style="color: #0000ff;">/&gt;</span>
	<span style="color: #0000ff;">&lt;mx:Label</span> id="cValue"<span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/mx:HBox&gt;</span>

<span style="color: #0000ff;">&lt;/mx:Panel&gt;</span></span></span></pre>
</blockquote>
<p>Basically, we create a Panel which contains two HBox to get user input and display the result from the web service. The only thing special here is the <strong>Click Event</strong> of the Submit button.</p>
<blockquote>
<pre><span style="color: #000000;"><span style="color: #000000;"><span style="color: #0000ff;">&lt;mx:Button</span> id="submit" label="Convert"
    click="<span style="color: #ff0000;">myWebService.FahrenheitToCelsius.send(fValue.text)</span>"<span style="color: #0000ff;">/&gt;</span>
</span></span></pre>
</blockquote>
<p>Here we refer to our Flex WebService through its id <span style="color: #ff0000;">myWebService</span>, call its operation <span style="color: #ff0000;">FahrenheitToCelsius<span style="color: #000000;">,</span> </span>and pass a value coming from the TextInput control <span style="color: #ff0000;">fValue</span>.</p>
<p>Now let&#8217;s click Run button in your Flex builder and see what will happen&#8230; &#8230;</p>
<blockquote><p><strong><span style="color: #000000;">Congratulations! You Have Created a Web Service Powered Flex Application!</span></strong></p></blockquote>
<p><span style="color: #ff6600;"><span style="color: #ff6600;"><strong>Recap what you have learned so far</strong></span></span></p>
<ul>
<li><span style="color: #ff6600;"><span style="color: #000000;">You can consume web services in Flex simply through <strong>&lt;mx:WebService&gt;</strong>.</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">You can specify <em>wsdl url, operation name, result event handler, fault event handler</em> for <strong>&lt;mx:WebService&gt;</strong>.</span></span></li>
<li><span style="color: #ff6600;"><span style="color: #000000;">You can trigger your WebService through an event (like button click event), call the send function of the operation, and pass parameters if needed.</span></span></li>
</ul>
<p>Through the sample application we built today, I hope you will see how easy it is to consume web services in Flex.  You can <a href="http://flextutorial.org/wp-content/uploads/2009/03/FlexWebService.mxml">download the complete source code here</a>.</p>
<p>If you have any questions, please feel free to comment on this post or <a href="http://flextutorial.org/contact/">contact me directly</a> .</p>
<p>For the next tutorial, we will talk about some details in Flex WebService and help you thoroughly understand it from A to Z.</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://flextutorial.org/2009/03/04/creating-a-web-service-powered-flex-application-in-one-minute/&amp;t=Creating+a+Web+Service+Powered+Flex+Application+in+One+Minute&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://flextutorial.org/2009/03/04/creating-a-web-service-powered-flex-application-in-one-minute/&amp;title=Creating+a+Web+Service+Powered+Flex+Application+in+One+Minute&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript">tweetmeme_url='http://flextutorial.org/2009/03/04/creating-a-web-service-powered-flex-application-in-one-minute/'; tweetmeme_style = 'normal';tweetmeme_source = 'flextutorial'; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://flextutorial.org/2009/03/04/creating-a-web-service-powered-flex-application-in-one-minute/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
