Webservices, simple introduction.

Website exposes their data to 3rd part application trought webservices and xml. It’s pretty simple: the user calls a url which has some parameters that contains the request, the webserver handles the request, queries the database and prints out the resulting data in XML (or JSON, or other) format. This permits to limit user access to the complete data, exposing only a part of them.

This is the underlying technology that empowers all kinds of mashup: you request data from some sources, combine it and show them up in your application. I want to share some examples of building an app that works with data loaded from external webservices; I’m not gonna show you another flickr example, there are too many out there, so I’d prefer doing something using Last.fm APIs.

First thing is, you have to apply for an API KEY. What does this means? Api calls somethines aren’t free, other times the provider wants to keep track of who calls which api, or keep statistics; So you have register a unique id that will sign all your calls. Usually you pass this key as a parameter of the function.

So to start, let’s choose a simple function to call, artist.getTopFans sounds good and doesn’t require authentication. It prints out informations about the top fans of a given artist. The documentation page tells that it requires 2 parameters: the artist name, and the api key, they explains themselves.

So now we know that we want to know who are the top fans of.. let’s say, Cher. We have to take the root url of the APIs, which is

 

http://ws.audioscrobbler.com/2.0/

 

enqueued by the parameters written as &key=value pairs (It’s a simple HTTP GET request, other services will require HTTP POST requests, but, later).
We now have the url of the call, which should be similar to:

 

http://ws.audioscrobbler.com/2.0/?method=artist.gettopfans&artist=cher&api_key=xxxxxxxxxxxx

You can try substituting your api key and open that url in a web browser.

The stage property and the display list

In Actionscript, the stage object is referenced by the DisplayObject.stage property. I use it alot, for example, to center elements on the screen, but as we read in the api reference,

If a display object is not added to the display list, its stage property is set to null.

this can be uncomfortable when you want to position something before attaching it, for any reason, or if you want a control object to operate on some stage property (fullscreen, for example).

So to avoid this, I usually add a static property to my Document Class, let’s call it staticStage:

public class FlashExample extends MovieClip {

    public static var staticStage:Stage;

    public function FlashExample():void {
        staticStage = stage;
[...]

Of course, the stage in FlashExample will not be null, since the DocumentClass is always added to the display list.

With this trick, you always have the stage at hand, by simply referencing FlashExample.staticStage anywhere on your code.

Last.fm Actionscript API

One of the social networking sites i find most useful is Last.fm; I won’t go into details, but i’ve decided to write down Actionscript API to provide a simple access to the Last.fm API.
I’ve just begun and i’ll consider this an exercise to learn something.

The big problem for the moment seems the authentication, i want to came out with something really simple to use, and i don’t like using evenListeners to fetch replies (i know.. i know.. but..).

So for the moment i’ve got only a bunch of functions working that don’t requires auth, like artist.getSimilar. See the example.

Ok i’m so proud this stupid example is my first first flex related thing that i show up to someone! :-)