Google's Android App Inventor is great fun to play with and I think represents a fascinating leap forward in mobile application development. It provides a whole toolbox of ready to go functionality, but sadly missing from this toolbox are facilities to interact with web sites and services (with the exception of Twitter of course). However there is one component that provides a glimmer of hope and if you want to publish data in a way that can be used by Android App Inventor developers then you can use this component with a PHP script to easily pass data to the mobile device.
The component in question is called tinywebdb and can currently be found in the “Not ready for prime time” section of the palette. This positioning might of course mean that this component could change over time and hopefully it will become more powerful. It is designed to allow you to store simple key value pairs on a web location. The reference page for the component gives you further details about it and provides sample code for a service that can run on Google AppEngine. I also found lots more useful information on building applications that talk to the web on the App Inventor API site which includes examples such as how to parse an RSS feed and pass the results through the tinywebdb component. Again it has server side code that can run on Google AppEngine, but the PHP section is, at the time of writing, labelled “coming soon”. So what if you have an existing website written in PHP and would like to pass data without using AppEngine?
Fortunately tinywebdb is quite unsophisticated in its operation and it is quite easy to write scripts that can interact with it. The component can do two things: “get” a value associated with a tag and “set” a value associated with a tag. It doesn't seem to pass any information so you may need to cleverly construct the values to be able to identify requests that should be associated with a user account or instance of your application.
When you use the tinywebdb component it needs a “ServiceURL” property setting. This is the web location where your script sits, e.g. http://example.com/wdbtest. At this location it expects to be able to call two URLS, both starting with the ServiceURL and then /getvalue or /setvalue appropriately. In many web server installations PHP scripts must end with “.php”. So our scripts for handling getting and setting of values would be called getvalue.php and setvalue.php. We cannot ask tinywebdb to call these URLs directly so we must tell the web server that when it sees a URL ending in “/getvalue” it should call “/getvalue.php”. To do this we will use Apache's mod_rewrite module. Ensure that this module is switched on in your webserver's configuration. We can now add some entries in .htaccess (or in the main Apache configuration file). If our web service was located in the “wdbtest” directory we could add some lines like this:
RewriteRule ^getvalue$ getvalue.php
RewriteRule ^setvalue$ setvalue.php
These lines switch on the rewrite engine, ensure that handles our subdirectory and then maps the incoming URLs to URLs that can actually be handled. This is transparent to the client. Now that we have got the request to a script we need to write some code to handle it. The request data itself is actually part of a POST request. The value of “tag” is posted for getvalue requests. For setvalue “tag” and “value” are both set. Responses are sent back to tinywebdb in JSON format with the first value being a string literal “VALUE”. The code below will respond to a request for any value with the current UNIX time:
$retval = array(“VALUE”, $_POST['tag'], time());
If you only wanted to respond to a specific tag you would need to include a test to check that $_POST['tag'] was set to the name of your tag and in the return array the string of the name of the tag should appear where $_POST appears now. More usefully of course your script could perform a database lookup for that tag, or call a remote API. If you want to indicate that the tag was not found set the value to an empty string.
Setting values should be similar, a script could start with:
$tag = $_POST['tag'];
$value = $_POST['value'];
// do something with the values
I haven't experimented much with setting values yet. If you were using a framework like Drupal you could probably write an add in that would handle these URLs without the need for the rewrite rules.
Hopefully it would be possible using this technique to make data available to AppInventor users in a way they could easily deal with in their applications. This might be really intresting for novice programmers, children programming for the first time, or even those looking to rapidly build prototypes of exciting new mobile applications based around your data.