Android

The following is an example of how to use NextDB.net inside the Android platform. We altered the Google Android NotePad tutorial to use NextDB.net for all of the database calls.


Overview

As mobile application development becomes more popular, the need for off-board storage tools and resources will become more necessary. Storing user data locally on the phone is dangerous because it can become easily corrupted, the phone can be lost or damaged, and most importantly the data cannot be shared. With the new NextDB.net Android API, you can now add full relational database access to your android application where all of your data is stored safely on the NextDB server. In conjunction with the NextDB.net JavaScript API and REST API, you can now interact with your data from virtually anything that has a network connection. Using NextDB in your mobile applications gives you a real advantage because you can easily surface all of the users data on the web. Your mobile application can have an associated web application, both speaking to and communicating through a shared database.


NextDB.net released a Java API for use within Java GUI applications and JSP/Servlets. We have extended that API to use within the Android programming framework. To simplify the learning curve we have taken the main NotePad tutorial from Google's documentation, which uses a SQLlite database on the phone, and replaced all of the database calls to use the NextDB.net API. The NotePad example illustrates database queries, inserts, updates and deletes. If you are new to Android, we suggest first reading the Google NotePad tutorial at:


http://developer.android.com/guide/tutorials/notepad/index.html


You can get all of the NextDB.net Android example source code at:


http://code.google.com/p/nextdb/source/browse/#svn/trunk/mobile/android/


The database used in the code is a shared example database, which you can import into your NextDB.net account with this URL:


http://nextdb.net/nextdb/service/brenthamby/DROID_NOTES/EXPORT


The demo data is browsable here:

http://nextdb.net/nextdb/rest/brenthamby/DROID_NOTES/NOTES/descending/timestamp



Getting Started

We created a new NextDB database with one simple table, called "NOTES". The difference between this table and the table used in the SQLlite example is that we have added an MSISDN column used to identify the phone (i.e. the phone number). You can run the code right out of the box, but beware that you are accessing a shared example database. To get your own account please go to http://www.nextdb.net where you can get a free 30 day trial. You will notice that there is only one table and one query in the database.


One of the other modifications is that we added all of the Database calls to run in a separate thread using Android AsyncTask. In other mobile frameworks like J2ME, you normally create and manage your own threads that report back to the main UI thread, however the Android framework restricts this and provides a variety of tools for making calls off the main UI thread, which can report back. We have added a private inner class for each network operation FetchAllNotesTask (Query), CreateNoteTask (Insert), UpdateNoteTask (Update) and of course DeleteNoteTask (Delete). Here is an example:


   private class FetchAllNotesTask extends AsyncTask<Void, Void, Void> {
       protected Void doInBackground(Void... unused) {
           try {
               nextDB.fetchAllNotes(phoneNumber);
           } catch (JSONException e) {
               // callback to the UI thread to re-populate the page
               reportError();
               Log.e("ERROR",e.getMessage());
           }
           return null;
       }
       protected void onPostExecute(Void result) {
           super.onPostExecute(result);
           // callback to the UI thread to re-populate the page
           populateList();
       }
   }

The other change to the original Google example is that we have replaced the SQLlite database wrapper class: NotesDbAdapter.java with NextDBAdapter.java, which basically replaces all of the SQLlite calls with NextDB.net calls. The example has the NextDB.net java API which encapsulates all of the the network calls.



Screen Shots



loading emulator



main list of rows from NextDB



entering new row



inserting back to server