Revisiting the HTTPService Classes
Ok, So HTTPService piece of cake right? You declare it in mxml and you set the url property and when you are ready you call send() and maybe if you need to pass some parameters. Couldn’t be any easier.
Except what if you want to use the HTTPService class from ActionScript? Well its not much harder. Just declare a new instance of the HTTPService object and… wait code completion tells us there are two HTTPService classes in the framework. One is in the mx.rpc.http package and the other is in the mx.rpc.http.mxml package. Seems like a pretty clear line there huh? One is for MXML and the other is not. But the problem is in the little things you might have gotten used to by using HTTPService in mxml, things like the showBusyCursor property. That property and its functionality are defined in the mxml package version of the class. Ok, well easy enough, import the HTTPService class from the mx.rpc.http.mxml package and use this to your hearts content. All is well that ends well and now we can move on.
Except… there are a lot of properties on the HTTPService object that are strings. But not any string will do. Think about resultFormat, and contentType. Compiler wont error by typing the wrong strings but at runtime we will get a yucky error if we have a typo. So the solution is to use some of the static constants that are declared on the mx.rpc.http.HTTPService object. This fixes the problem by giving us compiler warnings for invalid options. But you will notice above we decided to use the HTTPService object from the mxml package, and it doesn’t have these constants declared. So now we have a new problem. To use good practices and make use of the constants declared we have to use the main HTTPService object (can’t import more than one class with the same name) but then we loose that cute showBusyCursor feature. What will we do?
Not to fret, we can do on our own what the showBusyCursor was doing for us. Using the CursorManager class we can call the static method CursomManager.setBusyCursor(). This causes that cute little clock to show up for us. Then in our result event for the service call we use the static method CursorManager.removeBusyCursor().
So in the end… use the mx.rpc.http.HTTPService class from ActionScript. Its better for programmers and you can make up for what is missing the mxml version of the class.
28 May 2008 Simeon

You imply in the above that there is a difference between calling the HTTPService in mxml code and in ActionScript functions with this sentence:
Except what if you want to use the HTTPService class from ActionScript? Well its not much harder. Just declare a new instance of the HTTPService object and…
Do you have an example of how to do this? I have a service that I want to call from AS code, and I can’t get it to work correctly.
Thanks,
Louise
Hi Louise,
The trick that makes it not “much” harder is that there are actually 2 different HTTPService classes when you look at it from an actionscript point of view. There is the base HTTPService class and then another that extends it and adds some functionality for it to be used from mxml.
So what you are looking for is something like this.
import mx.rpc.http.HTTPService; private hs:HTTPService = new HTTPService(); private function init():void { hs.url = 'http://someurl.com'; hs.addEventListener('result', onResult); } private function onResult(event:ResultEvent):void { trace(event.result); }Of course I just typed that on the fly but it should get you close.
So I guess this explains the compile time errors when trying to use mxml httpservice calls and as3 created calls in the same mxml document. Makes sense.