nwebb

Flex, Flash, AIR

Cairngorm Webservice & HTTPService Examples

In my previous post I uploaded an example application which used an HTTPService within Cairngorm.
Here I'll compare the HTTPService code with equivalent WebService code so you can easily see the differences (there are very few):

HTTPService
1. You locate the specific service using the ServiceLocator singleton and the id of your HTTPService.
2. When you are ready, you call the send() method of the HTTPService (i.e. service.send()) passing in any required parameters.
3. Next you declare the responder methods for that service (i.e. these are the methods which will be called on success/fail of the server request - note: they have nothing to do with this.responder which actually refers to your Command instance ... confused yet?!).

Okay, I'll do my best to clear this up......
You need responder methods so that your asynchronous operation (the call to the server) knows which methods to call depending on whether the operation was successful or not. You can declare the responder methods using the mx.rpc.Responder class, but perhaps a cleaner way is to make your Delegate class implement the IResponder interface. Both ways are shown below:

Actionscript:
  1. //ONE OPTION FOR DECLARING RESPONDER METHODS:
  2. public class SomeDelegate implements IResponder
  3. {
  4.     public function doSomething(){
  5.         var token:AsyncToken = this.service.send( {//send in stuff} );
  6.         token.addResponder(this); //YOU CAN DO THIS BECAUSE CLASS IMPLEMENTS IRESPONDER INTERFACE, AND SO RESULT/FAULT WILL GET CALLED
  7.     }
  8.  
  9.     public function result(obj:Object):void{ //do something if server request is successful  }
  10.     public function fault(obj:Object):void{ //do something if server request fails  }
  11. }
  12.  
  13. //=====================================================================
  14.  
  15. //ANOTHER OPTION FOR DECLARING RESPONDER METHODS:
  16. public class SomeDelegate
  17. {
  18.     public function doSomething(){
  19.         var token:AsyncToken = this.service.send( {//send in stuff} );
  20.         var responder:mx.rpc.Responder = new mx.rpc.Responder( loginResult, loginFault );
  21.         token.addResponder(responder); //CLASS DOESN'T IMPLEMENT INTERFACE, SO YOU NEED THE LINE ABOVE
  22.     }
  23.  
  24.     public function loginResult(obj:Object):void{ //do something if server request is successful  }
  25.     public function loginFault(obj:Object):void{ //do something if server request fails  }
  26. }

Note: If you do it the first way then both your Command class and Delegate class are implementing the IResponder interface.

Here is the example using the HTTPService:

Actionscript:
  1. //Constructor function for class LoginDelegate
  2. public function LoginDelegate( responder : IResponder )
  3. {
  4.     this.service = ServiceLocator.getInstance().getHTTPService( "aaa" ); //note: getHTTPService specifically called
  5.         this.responder = responder; //the responder is a reference to your Command class
  6. }
  7.  
  8. public function login( loginVO : LoginVO ): void
  9. {
  10.     var token:AsyncToken = this.service.send( {username:loginVO.username, password:loginVO.password} );
  11.         token.addResponder(this); //the LoginDelegate class implements mx.rpc.IResponder and so it will declare "result" and "fault" methods
  12. }

In Services.mxml you need to declare your HTTPService. Note how the id corresponds to the above ServiceLocator call.

Actionscript:
  1. <!-- HTTPService declared in Services.mxml -->
  2. <mx:httpservice id="aaa">
  3.         url="http://someurl/login.php"
  4.         showBusyCursor="true"
  5.         method="POST"
  6.         resultFormat="text"
  7.  />

WebService
1. You locate the specific service using the ServiceLocator singleton and the id of your WebService.
2. When you are ready, you call the name of an operation on the WebService (i.e. service.doLogin()) and pass in any required parameters.
3. Next you declare the responder methods for that service (see notes in above example).

Actionscript:
  1. //Constructor function for class LoginDelegate
  2. public function LoginDelegate( responder : IResponder )
  3. {
  4.     this.service = ServiceLocator.getInstance().getWebService("bbb"); //note: getWebService specifically called
  5.         this.responder = responder; //the responder is a reference to your Command class
  6. }
  7.  
  8. public function login( loginVO : LoginVO ): void
  9. {
  10.     var token:AsyncToken = this.service.doLogin(loginVO);
  11.         token.addResponder(this); //the LoginDelegate class implements mx.rpc.IResponder and so it will declare "result" and "fault" methods
  12. }

In Services.mxml you need to declare your WebService. Note how the id corresponds to the above ServiceLocator call and how the WebService has a number of operations:

Actionscript:
  1. <!-- WebService declared in Services.mxml -->
  2. <mx:webservice>
  3.     id="bbb"
  4.     wsdl="{model.config.someWsdlUrl}"
  5.     useProxy="false"
  6. >
  7.     <mx:operation concurrency="multiple" name="doLogin" resultformat="e4x" />
  8.     <mx:operation concurrency="multiple" name="doSomethingElse" resultformat="e4x" />
  9. </mx:webservice>

11 Comments so far

  1. Luís Eduardo February 5th, 2008 4:59 am

    thank you very much about this accurated and well explained post dude! I am struggling with cairngorm here and your help was VERY…helpfull :P

    take care (blog added)

  2. Roshan Amadoru March 3rd, 2008 12:44 am

    dude… this is great…. i just got saved a lot of my time because of this.. i was so stuck with this iresponder attaching to token. :) i just got it solved.. thank you very much.

  3. [...] take a look at http://nwebb.co.uk/blog/?p=118 [...]

  4. kire July 16th, 2008 3:26 pm

    Hello

    I would have a question. When you do a httpservice with cairngorm, and that you want to execute a function that modify a component on your mxml file AFTER the httpservice how can you do ?

    Often in a mxml, the function that modify a component is executed after the httpservice…

    Thanks

  5. nwebb July 17th, 2008 3:04 pm

    @Kire - if I understand you correctly you want to modify a component after you have retrieved the results from an HTTPService call? If so, the general flow with Cairngorm is that in the resultHandler of your HTTPService, you update the model with the info you’ve retrieved. Your view components are bound to the model, so the view updates itslef when the model changes. If you prefer you can use ChangeWatcher directly to trigger methods when values in the model change.

  6. Adobe Flex - Cairngorm « Panduramesh’s Weblog September 29th, 2008 10:49 am

    [...] Neil Webb :: Cairngorm Webservice & HTTPService Examples [...]

  7. anil4it September 30th, 2008 1:19 pm

    [...] Neil Webb :: Cairngorm Webservice & HTTPService Examples [...]

  8. Anilkumar September 30th, 2008 1:46 pm

    [...] Neil Webb :: Cairngorm Webservice & HTTPService Examples [...]

  9. It’s all about RIA October 14th, 2008 6:26 am

    [...] Neil Webb :: Cairngorm Webservice & HTTPService Examples [...]

  10. Clint November 21st, 2008 10:55 pm

    I am making the assumption that

    model.config.someWsdlUrl

    is a static public var? so this example does not use the Data,Import Web Service option? If it does, I am not doing a good job of following!

    I am very new!

  11. Anselm Bradford December 6th, 2008 5:22 am

    Do you know that the MXML examples you are showing are using lowercase for the class names… e.g. <mx:httpservice and <mx:webservice … which will cause compilation errors.

Leave a reply

Bad Behavior has blocked 1077 access attempts in the last 7 days.