FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Mobility
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
June 08, 2007

Flash Lite: Graphics for Mobile Devices

(Page 3 of 6)

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&";
?>
Listing Two

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.

Previous Page | 1 Flash Lite | 2 Which Flash Lite Version to Target? | 3 Preparing the Data Service | 4 Getting Started with Client Applications | 5 Testing the Application | 6 Adding the Final Code Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK