October 15, 2008
by Keenan
Recently, Branden and I were tasked to build a Flash UI on top of a web service. Seems like pretty standard development…but, as you have to know by now, nothing is ever “standard” at Automata. After some research, trial & error, and hand-wringing, we realized that the device we were attempting to connect to was using SOAP 1.2. Well, unfortunately, Flex only supports up to SOAP 1.1, so we had a bit of a problem.
It didn’t take us long to remember that SOAP was just XML, and that Flex’s web service API was just a nice class that makes it easy to speak that language. After deciding to nix the Flex solution (we were only using Flex for the WebService API), we moved to an all Flash solution. We created a connector class to act as a web service API, and simply sent XML packets directly to the server using URLLoader. One note about this solution is that it will only work if you have knowledge of the web service you are trying to use. In our case, we had the entire schema at our disposal, so we knew the exact format of the service operations.
The connector class contains public methods to call the individual web service operations. Additionally, we created individual request classes for each of the operations that extended a class called Abstract Request. AbstractRequest provides a method send(uri:String, headers:Array) that uses URLLoader to send the request to the server.
The “uri” parameter of send() is the full path to the web service, with the “http://” attached. The headers are the HTTP headers that you need to send to the web service for such things as authentication. In our code, these are set in the Connector class, and passed to each request as it is needed. The “_request” property holds the XML object that you will pass to the server. This XML is formatted as an XML packet specific for this web service. The value for this property is defined in the AbstractRequest sub-classes.
The _request property is an XML object that defines the format, and uses Flex-style binding to add the actual data from the constructor parameters. This is a little-known XML trick you can do in AS3. Branden found it in the Help section of the Flash CS3 IDE. Pretty neat if you ask me.
Back to the AbstractRequest class, we simply set the data property of the URLRequest intstance to be the string representaton of the request XML, and call URLLoader.load(), passing in the URLRequest instance. This sends the XML packet to the server. The handler for Event.COMPLETE will receive the XML returned from the server.
Our connector class has methods like:
1 2 3 4 5 6 | public function addGroup(groupId:String, groupName:String, flag:String):void { var request:AddGroupRequest = new AddGroupRequest(groupId, groupName, flag); request.addEventListener(ISYResultEvent.EVENT, onResult); request.addEventListener(ISYFaultEvent.EVENT, onFault); request.send(_uri, _headers); } |
This actually executes the web service call.
And just like that, you’ve build your own web service class. While it’s quite specific to the web service you are using, you could certainly expand it to consume a WSDL file so you can use it for any web service.






1 comment
Hmm. Can you go into more information on where you found the Flex style binding of your “_request” property? I’m not knowing what to look for.