Requesting Webservices in Flex 3

Follows Webservices, simple introduction.

In Flex 3 requesting data is really simple, since you really have to load a well know url which prints out XML (or JSON or whatever).

I assume you have read the introduction, here we are with a url that prints out informations about Cher’s top fans:

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

What we have to do is load that url with a mx:HTTPService Object and parse the result as XML. Simple, it isn’t?

First: create the HTTPService object:


GeSHi Error: GeSHi could not find the language mxml (using path /nfs/c03/h01/mnt/85630/domains/nightdrops.com/html/wp-content/plugins/snipplr/geshi/geshi/) (code 2)

and the handlers


GeSHi Error: GeSHi could not find the language actionscript3 (using path /nfs/c03/h01/mnt/85630/domains/nightdrops.com/html/wp-content/plugins/snipplr/geshi/geshi/) (code 2)

GeSHi Error: GeSHi could not find the language actionscript3 (using path /nfs/c03/h01/mnt/85630/domains/nightdrops.com/html/wp-content/plugins/snipplr/geshi/geshi/) (code 2)

And oplà, in the data field you will have the returned xml, ready to be shown in your preferred component!

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.

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! :-)