Update 6/14/11: the samples here are in updated form at: appinventorapi.com. The samples use the tinywebdb component in app inventor, and require the building of a server app (in app engine). Note that there is also a new web component in app inventor where you can directly talk to an api. See my sample at: http://appinventorblog.com/2011/06/02/app-inventor-has-a-new-component-the-web/ and the more complex sample at appinventorblocks. Your App Inventor Android apps can communicate with a web service API using the TinyWebDB component. TinyWebDB can only talk to an API that follows a particular protocol. That protocol is not published, but the samples attached at the bottom of this page follow it and can be used to create new "App-Inventor-compliant" APIs. Sample APIs Three samples APIs are attached at the bottom of this page.:
Downloading and Testing Sample 1. Download "tinywebdbapi.zip" from attachments at bottom of this page. 2. Unzip into a folder "tinywebdbapi" 3. Go to your App Engine site at appspot.com (register/log in if you're not) and create a new application. Remember the app id you give the new app. You might name it for particular api you'd like to build. 4. Open the app.yaml file in the folder "tinywebdbapi". Change the Application entry to the app id you used when you created the new app at appspot.com. 6. At a terminal, go to the parent folder of "tinywebdbapi"). Run the command: "appcfg.py update tinywebdbapi". This will upload your app to Google. 7. The samples have both a web page interface and an API interface. Open a browser, enter "localhost:8080" and test the app. You can click get_value and enter a tag to see the result. Test Sample with App Inventor App 1. Create a new App Inventor project. 2. Add a texbox, a label, a button, and a TinyWebDB component. 3. Modify the TinyWebDB component's ServiceURL property. Set it to the URL of the API you just created. 4. In the blocks editor, code the button.click event to call TinyWebDB.getValue. Plug in the textbox.text (what the user enters) into the getValue function. 5. Drag out a TinyWebDB.GotValue event-handler. Inside it, plug the valueFromWebDB into your label. 6. To test, connect your phone, enter something in the text box and click the button. The sample should just display your tag back along with "somevalue". Edit the sample Open the file main.py in the tinywebdbapi folder. Search or scroll down to find the class GetValue and its function get_value. This is the function you need to modify to customize this code. You can perform any computations you like in this function, with your end goal setting the variable "value". It is sent as one of the parameters in the WritePhoneOrWeb function call on the last line of the function. Your value can be a string or a list. If it's a string, it needs quotes around it so that App Inventor will read it correctly. In Python, you add quotes to a string variable value with: value = "\""+value+"\"" You'll see this line in the sample. Try modifying the code in a simple fashion, then uploading (appcfg) to Google and re-testing on the phone. Calling some other API Your API code can perform any computations, but one common purpose of these APIs will be to serve as proxies to some other API, essentially translating the data from whatever format to the TinyWebDB format. Try customizing the sample API by calling another API within the get_value function. One to try, if you know some Python, is the Yahoo Finance API. Here's a sample call to it: http://download.finance.yahoo.com/d/quotes.csv?f=sl1d1t1c1ohgv&e=.csv&s=IBM It's returns a simple string (not XML). But be careful, the string it returns has embedded quotation marks. You'll need to split the string and then strip the quotation marks from each part. |


