Table of contents
- Web Service small how-to
- Flex and PHP web-service
Flex3 offers a nice way of using web-services. Flex2 does not have the feature to the generate wsdl classes, but there is a way to write one general function that allows you to connect to any webservice and pass any number of parameters to it. Here is the quick and dirty code :
private var _wsGeneralService:WebService; private var _wsGeneralMethod:String; private var _wsGeneralParam:Object; public function connect(webserviceURL:String, webservice:String, webmethod:String, params:Object) { _wsGeneralService= new WebService(); _wsGeneralMethod = webmethod; _wsGeneralParam = params; _wsGeneralService.wsdl = webserviceURL+ webservice+ ".asmx?WSDL"; _wsGeneralService.addEventListener(FaultEvent.FAULT,errorHandler); _wsGeneralService.addEventListener(ResultEvent.RESULT,handleResult); _wsGeneralService.addEventListener(LoadEvent.LOAD,loadHandler); _wsGeneralService.loadWSDL(); function errorHandler(event:FaultEvent):void { // General Error Handler if (event.message) { Alert.show(event.message.toString()); } else { Alert.show("Unknown error!"); } } } private function handleLoad(event:LoadEvent):void { _wsGeneralService.getOperation(_wsGeneralMethod ).arguments = _wsGeneralParam; (_wsGeneralService.getOperation(_wsGeneralMethod ) as Operation).resultFormat = "e4x"; wsGeneralService.getOperation(_wsGeneralMethod ).send(); } private function handleResult(event:ResultEvent):void { var __response:XML = event.result[0] as XML; var resultNamespace:Namespace = __response.namespace(""); default xml namespace = resultNamespace; var data:XMLList = __response.child(_wsGeneralMethod+"Result"); default xml namespace = new Namespace(""); // now use data variable to parse the SOAP response. ... ... }
Note1:
params – is an instance of a class that has public variables (or getters) with the same name as the parameters required by the webService. So if the WS requires param1(string) and param2(int) then the class must have 2 getters: one named param1 that is of type string and one named param2 that is of type int.
Note2:
The line
(_wsGeneralService.getOperation(_wsGeneralMethod ) as Operation).resultFormat = "e4x";
informs that we want to parse the result as a E4X structure.
Note3:
You need to pay a close attention to the namespaces used. A good approach is to have something like the following in the handleResult code (where the dots are now):
var ns:Namespace = data.namespace(""); Alert(data.ns::ReturnParam);
Tags: ActionScript, flex, webservice
This post was written by Virgil Cristea
Views: 7450










Yea, but nor Flex 2, nor Flex 3 can’t handle authentication issues. I mentioned to Adobe already few times, but problem still remains.
Article about authentication in Flex and speaking also about web service authentication can be found here.
Citation: You can only configure authentication for named services. There are three options for handling a service endpoint URL that is secured using Basic authentication: pass-through, custom, and run-as authentication. Each of these options only works when the endpoint URL is secured using Basic authentication.
I have not tried auth but here is what I think how it can be achieved:
or: