Preparing the Data Service
Unless you use an existing data service, you need to set up a server on the Internet to provide data to your application.
Again, the simplest solution is to send a text file composed of name-value pairs as a response to the HTTP request coming from the Flash Lite application. The file can be a static file, but you can create more compelling applications using data from, say, a MySQL database. The format for this text file is "<name>=<value>&" where the ampersand (&) separates one name-value pair from the next pair.
When a Flash Lite application receives a response containing a list of name-value pairs, it stores that data by creating variables called "name" containing the "value" as their value. The Flash Lite LoadVariables() method lets you point out the object where the data is to be stored. This loose encapsulation helps keep the application manageable.
In my example, all that's needed on the server side is a PHP script, which retrieves the requested data from the SQL server, then properly formats it. Listing Two publishes yearly top-five salary and homerun lists as name-value pairs from a SQL server. The data in this example is imported from www.baseball-databank.org. The database is huge, but we are using just a small subset of the information in it.
<?php // create the SQL query based on query type and the year // there is a hardcoded 5 row limit (returning only top 5) switch ($_GET["query"]) { case "homeruns": $sqlquery = "SELECT nameFirst, nameLast, HR AS value FROM master, batting WHERE master.playerID=batting.playerID AND batting.yearID= '" . $_GET["year"] . "' ORDER BY HR DESC LIMIT 0,5"; break; case "salaries": $sqlquery = "SELECT nameFirst, nameLast, ROUND(salary) AS value FROM master, salaries WHERE master.playerID=salaries.playerID AND salaries.yearID= '" . $_GET["year"] . "' ORDER BY salary DESC LIMIT 0,5"; break; default: die("Incorrect query requested"); } // make the SQL connection ready mysql_connect("<your mysql server address here>", "<your mysql username here", "<your mysql password here>") or die("Could not connect to database"); mysql_select_db("baseball") or die("Could not select database"); // make the SQL query $result = mysql_query($sqlquery) or die("Could not complete query\n&noData=true&\n&loaded=true&"); // print out the resulting rows as name-value pairs, // limited with &-characters $i = 1; while ($row = mysql_fetch_array($result)) { echo "&name$i=" , $row["nameFirst"] , " " , $row["nameLast"] , "&\n"; echo "&value$i=" , $row["value"] , "&\n"; $i++; } // turn on the noData flag if result set was empty if ($i == 1) echo "&noData=true&\n"; // turn on the flag indicating that all data has been loaded echo "&loaded=true&"; ?>
The script adds one additional variable loaded at the end of the data. Since loading data over a cellular network can take a few seconds, the client should show a "loading data" page to the user. The solution here is to add an extra variable to the end of the data file, which is also updated on the client side. When this variable is set, the client knows all data has been received. If there are no results for the query, a noData variable is sent to the client, so that an empty results page is not shown to users.