Fx{r} is trying to start the Fx{r} Community! Please join our group on Adobe Groups following this link: http://groups.adobe.com/groups/ab29539ab9.
Fx{r} is now on Twitter too. Follow us @ twitter.com/fx_r!
«
»

ActionScript, Web Service

Web Service small how-to

Virgil Cristea | 23.10.07 | 3 Comments

Table of contents

  1. Web Service small how-to
  2. Flex and PHP web-service
Google Buzz

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);
Share and Enjoy:
  • Twitter
  • Google Buzz
  • LinkedIn
  • Google Bookmarks
  • del.icio.us
  • Digg
  • Sphinn
  • blogmarks
  • Reddit
  • StumbleUpon
  • Facebook
  • DZone
  • FriendFeed
  • Yahoo! Buzz
  • Yahoo! Bookmarks
  • Slashdot
  • MySpace
  • Add to favorites




Tags: , ,

This post was written by Virgil Cristea

Views: 7450

related

3 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

:

:


«
»