Shaun Mccran

My digital playground

02
A
P
R
2009

Adobe AIR Web Service Hello World test application

I've recently been looking at putting together some AIR applications. I've used FLEX for a few years now, and have only just come up with some useful AIR ideas, so I thought I'd build an application or two.

Usually I would use flash remoting, but I haven't spent too much time investigating how this works in AIR, so I've opted for the old school Web Service.

In the middle of my newest AIR application I stumbled upon an issue. No matter what I did I was receiving a 'HTTP Error' response from my Web Service. After stumbling around in the dark for a while tweaking code to no avail, I decided to write the most basic Web Service I could think of.

So here is 'Hello World', as a Web Service call from AIR.

Firstly create a call to your Web Service. In this case it was a local file. Point the wsdl variable at the fully qualified path to your service. I am using a coldFusion back end, so it is a CFC. This is also where you specify the fault handler and result handlers. You can add as many 'operation' methods here as you want, that way you address specific functions in your service individually.

view plain print about
1<mx:WebService id="getMessages" wsdl="http://192.168.XX.XXX/root/services/message.cfc?wsdl" showBusyCursor="true">
2 <mx:operation name="sayHello" fault="faultHandler(event)" result="resultsHandler(event)" />
3 </mx:WebService>
4     <mx:Button x="10" y="10" label="Click me" click="getData()"/>
5    
6</mx:WindowedApplication>

I've also added a button that will call a function to action the service call.

Next we will add the functions.

view plain print about
1<?xml version="1.0" encoding="utf-8"?>
2<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
3
4<mx:Script>
5    <![CDATA[
6        import mx.rpc.events.ResultEvent;
7        import mx.rpc.events.FaultEvent;
8        import mx.controls.Alert;
9        
10 private function getData():void{
11     getMessages.sayHello();
12     
13 }
14
15     private function faultHandler(event:FaultEvent):void
16     {
17        Alert.show("Error: " + event.fault.faultString, "Application Error");
18     }
19
20     private function resultsHandler(event:ResultEvent):void
21     {
22        Alert.show(String(event.result))
23        
24     }
25    
26    ]]>
27</mx:Script>

A getter function, that will actually send the Service request, a fault handler that will simply Alert the user to a fault event, and a result handler that Alerts the user to whatever message is returned from the Web Service.

The CFC

The CFC service is a simply object to return a string. Just make sure that your 'Access' is set to remote.

view plain print about
1<cfcomponent displayname="message">
2
3
4    <cffunction name="sayHello" displayname="sayHello" hint="it says hello" access="remote" output="true" returntype="String">
5        
6        <cfset var message = "Hello world">
7        
8        <cfreturn message/>
9    </cffunction>
10
11</cfcomponent>

So we end out with:

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top