Passing data from a PHP script to an Android AppInventor application using tinywebdb

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. 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:

RewriteEngine On
RewriteBase /wdbtest
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:

// getvalue.php
$retval = array(“VALUE”, $_POST['tag'], time());
echo json_encode($retval);

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:

// setvalue.php
$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.



Thanks for the info Liam. I just received my invite to App Inventor last night and I'm trying to get my own TinyWebDB service running using PHP. I have tried implementing the examples you've provided and I keep getting a " Communication with the web service encountered a protocol exception". Did something change? Let me know if you have any suggestions. I wasn't able to find any additional help anywhere.

Hi Shane, nothing has changed as far as I know. Did you try out the web service in a browser first? I think it might be fussy about the format of the data so make sure the word "VALUE" appears as the first element in your JSON array. Also the json_encode function is only present in PHP 5.2 onwards I think so you might want to check what version you are using.

Really interesting! I am playing with App Inventor at the moment and thought I might try to use some data sitting in an HTML table (updated every few minutes) on our local council site. I have done some PHP in the past but I'm hardly an expert, and after a bit of searching I found an article about cleaning up HTML with the W3C Tidy service and Yahoo Pipes. That set me thinking - how hard would it be to build a pipe that accepts data (say RSS to begin with) and converts it to JSON in a way that TinyWeDB can read? I know this wouldn't allow for writing back, but if such a pipe existed it would be a doddle for novices (like me) to build apps that consume live data. Other pipes could consume other sources.

I don't really know ow viable this is. What do you think?

Hello everybody. After a long long search on the internet to find a solution to tinywebdb to local or global server connection problem, I found a product that provides PHP files to create your own tinywebdb locally or glbally running on your server.
You can access the page using the link below:

Add new comment

Comments are always very welcome, but please note the following:
  • Sadly due to the high number of spam comments recently all comments are now manually moderated. You comment will therefore not appear on the site instantly.
  • Comments on this web site are monitored for spam using Mollom. By posting a comment, you accept that your message and other personal details about you will be analysed and stored for anti-spam and quality monitoring purposes, in accordance with Mollom's privacy policy.
  • Please use your own name not a company or website name to submit comments. Your comment will be removed if you don't do this.
  • All links in comments will be marked with a no follow attribute. That means posting a link to your site here won't help your search engine rankings.
  • By submitting a comment you agree that your comment can be reproduced under the same licensing terms as the rest of the content on the site.
  • Comments can be removed at any time without explanation, but won't be removed just because you disagreed with something I said.