Search code examples
jsonactionscript-3flashapache-fleximdb

Imdb api with flash - as3 - flex


I am trying to use the imdb API, in this case: http://imdbapi.org/ I need to search for a movie by name and get a json, then load an image with the poster obtained. I'll be using as3 - flex to generate an Air package.

I tried this example, but can't seem to get it right.

import flash.net.*;
var url:String = "http://imdbapi.org/";
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.GET;

var variables:URLVariables = new URLVariables();
variables.name = "Pulp fiction";
request.data = variables;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);

function onComplete (event:Event):void {
    trace(event.target.data);
}

Perhaps you could enlight me with an example of connecting to the api and retrieving that json so that I can load an image with the poster and generate my air package.

Many thanks!


Solution

  • The API seems to want the movie in the q param so change this

    variables.name = "Pulp fiction";
    

    to :

    variables.q = "Pulp fiction";
    

    To verify : http://imdbapi.org/?q=Pulp%20Fiction

    From there getting the poster URL is just a matter of reading the correct property from the JSON string.

    private function onComplete (event:Event):void {
        var data:Array = JSON.parse(event.target.data);
        if(data && data.length)
        {
           var movie:Object = data[0];
           trace(movie.poster);
        }
    }