Note: This application was created using Adobe® Flash Builder® 4.0.1
Web Services are a great way to perform cross-platform communications and luckily it is very easy to implement in an Adobe® AIR® application. This sample application is created using MXML rather than pure Adobe® ActionScript® for simplicity's sake. This article shows how to create a fun application that leverages a simple web service implementation that returns random sentences constructed using the user-entered values (aka Mad Libs).
Within the <Declarations> tags we define our WebService object, myWebService, and set up its properties and default handlers. From below it can be seen that the server-side web service is to be located at "http://localhost:8181/madlibws?wsdl". The following attributes are also defined:
load - The method called when the server WSDL is successfully loaded
result - The method called when a web service result is returned from the server
fault - Should an error occur with myWebService, this method will get called and receive fault information
<!-- Declare our WebService object and define some default handlers -->
The next thing to take care of is providing a form for the user to input values to be used in our web service call. In this case we have a heading, fields for the various parameters, and a button to initiate the web service call.
The Button's click handler has been set to call the method generateButton_clickHandler(event) when the button is clicked. We also set the button to be disabled (enabled="false") by default until we know the server is accessible (see next section).
The myWebService_loadHandler method is called after the server has been successfully reached. At this time we know our application can communicate with the server, so we can enable the button which triggers the web service calls:
protected function myWebService_loadHandler(event:LoadEvent):void
//Enable our button so we can now make web service calls
Now that the button is enabled we can go ahead and click it. Thic click will result in a call to the generateButton_clickHandler method. Within this method we read the values from our fields and pass them all as parameters in our web service call:
protected function generateButton_clickHandler(event:MouseEvent):void
//Make the call to the web service object passing each string as a parameter
myWebService.generateMadLib(nameField.text, placeField.text, verbField.text, nounField.text);
After the server receives our web service call it will respond with a result. This result is handled automatically by the WebService class and passed to the registered handler, myWebService_resultHandler. In this case we are only receiving a String, so to display it to the user we just use an Alert box:
protected function myWebService_resultHandler(event:ResultEvent):void
In the event that any errors occur we want to make sure our end-user is made aware. In other applications this may want to be handled a bit more delicately, but in this case we are just going to display the error message in an Alert box to make testing a bit easier:
protected function myWebService_faultHandler(event:FaultEvent):void
Alert.show("Call failed: " + event.fault.faultDetail);
And that's it, the application is complete. You can use the attached code sample as a starting point to create an Adobe AIR application based on other existing web services, or build onto this application and improve the functionality and user interface.