adaam
adaam

Reputation: 3706

Passing data in AS3 to a Query String (API)

I'm writing a mobile app where a user can match ingredients to recipes. I have stumbled across http://www.recipepuppy.com/about/api/ which provides a very simple API to search by comma separated ingredients such as:

http://www.recipepuppy.com/api/?i=onions,garlic&format=xml

I have various ingredients stored in a shared object like so:

so.data.meat1 = "beef"
so.data.meat2 = "chicken"
so.data.meat3 = "lamb"
so.data.veg1 = "green beans"
etc etc..

I am completely new to AS3, so I don't really have an idea of the main methods / classes that could achieve this.

So 1. How can I pass my shared object's data into the query string of recipe puppy's url above? 2. How would I load the XML results into a datagrid or similiar component?

edit: this is what I have got so far:

var url : String = 'http://www.recipepuppy.com/api/';

            // url variables all which will appear after ? sign
            var urlVariables : URLVariables = new URLVariables ();
                urlVariables['i'] = so.data.meat1;
                urlVariables['i'] = so.data.meat2;
                urlVariables['format'] = "xml";
                // here you can add as much as you need

            // creating new URL Request
            // setting the url
            var request : URLRequest = new URLRequest  ( url );
                // setting the variables it need to cary
                request.data = urlVariables;
                // setting method of delivering variables ( POST or GET )
                request.method = URLRequestMethod.GET;

            // creating actual loader
            var loader : URLLoader = new URLLoader ();
                loader.load ( request );
                trace(request.data);

It's working well, but my current setup of the urlVariables['i'] only allows me to specify one 'i' variable, how do I specify multiple variable values for the 'i' variable?

Upvotes: 0

Views: 177

Answers (1)

OK, for an answer I need to enhance it:)

first you should store ingredients as an array in SO, like shown here SharedObject#data

so.data.ingredients = ["beef","chicken","lamb"];

then you can pass it to the urlVariables as:

urlVariables['i'] = so.data.ingredients.join();

of course I would add som tests if the so.data.ingredients exists to acoid any errors thrown,

Upvotes: 1

Related Questions