The first thing we’re going to to is jump right in,  write some code and build a small test application to connect to a web service, retrieve database data and display it in a table. The purpose of this is simply to test that the front to back stack works as hoped.

After this will take a bit of a step back and start taking a look at the overall design of the front end Android application.

Clone the code to a local repository using the following mercrial commannds.

$ hg clone https://sdass@bitbucket.org/sdass/databaseconnectivitytest ./<your_local_location>
$ hg update 23b7df5e9ffd

Create a project in Eclipse using the cloned project code.

The main files of interest are:

  • DbViewAllActivity.java – This activity opens an HTTP connection to  our web service that fetches all AnimalSightings rows, reads and decodes the POST data from the response and displays the information in a table.
  • activity_db_view_all.xml – Defines the GUI layout for the above activity.

For this to run a few things need to be setup correctly on the server:

Look at the code:


private BufferedInputStream openHttpInputStream() throws MalformedURLException, IOException {
// To use the host machine local host use 10.0.2.2, an alias
// that refers to the host machine' localhost the usual
// 127.0.0.1 would actually refer to the android emulator as
// localhost In production we'd get this URL from another
// resource somewhere.
String hostName = "simon-mintvm";
String webPath = "ZooBreakWeb";
String scriptName = "RetrieveAllSightings.php";
String urlString = String.format("http://%s/%s/%s", hostName, webPath, scriptName);


URL viewAllphpURL = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) viewAllphpURL.openConnection();
BufferedInputStream is = new BufferedInputStream(urlConnection.getInputStream());


return is;
}

The hostName, webPath and scriptName must all match what is actually on the server under the web root. If using an emulator use ‘10.0.0.2’ as localhost as explained in the comment.

If we’re successful, and assuming we have some rows populated in the database, on pressing the ‘View All’ button we should end up with something like this.

Screenshot_DbViewAllActivity

Next Page: Application High Level Design

Back To: Setting up the Android Development Environment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: