Android SDK: Making Remote API Calls

This tutorial will explain communicating with an external data source via an Android application, to be used when logging into a web based service or backing up data to an external source, among other things.

The external data can be stored in any of the many databases available today, which in turn can be hosted by any of the operating systems available. With this in mind, the theory becomes as important as the code.

We want to be able to send information from one platform to another completely separate platform and vice versa. It sounds simple, but there's a language barrier to overcome and there's not always an existing solution. We need a data type that both platforms can work with and some code in the middle to handle the requests. This is the API. The below diagram illustrates this better than words.

For this tutorial, I will be using a standard LAMP stack. The external database will be MySQL, and I will use PHP to make the API. The PHP file will write and read to the MySQL database based on requests received from the application. It will also send the results of the requests back to the app. The requests and responses will be in JSON as both parties understand this format. The goal is to enter a person’s name from the Android application and retrieve the person’s age from the external database. I know it's simple, but it’s a broad area and it's a good way to illustrate the process.

Note: I’m using a LAMP stack because it’s free and accessible. I will give further instructions in the download code about setting one up for those that require it, but I won't be going into great detail about this side. I will show the code and explain its function, but it would be a very long read to go into detail about PHP, SQL, JSON and so forth.
Also I am not touching upon security in this tutorial, I will most likely cover it separately in a follow up.

Creating the MySQL Database and Table

Connect to your MySQL instance however you prefer and perform the following.

Create the API

For the purpose of this tutorial, I will be using a single PHP file called android_api.php. This needs to be placed somewhere where it can be accessed via HTTP request. It might be in the www directory on a local LAMP or WAMP setup, or on a hosted server that you can access.

The app will send a tag to this page along with any additional parameters. The API will perform an action based on the content of the tag. In this basic example there is only one tag, one parameter, and one action, so it wasn't really necessary. Still, doing it like this will help you understand how to perform multiple tasks from the API.

Note: In practice you may well split things across several PHP files, so you might have one file for config parameters, another to handle database requests, and so on. In the interest of keeping things both simple and quick, I will stick with a single file.

Creating the Application

In the IDE, create a new Android project by going to File → New → Android Application Project. Next, name the project. I went with remoteDB. From here on out, you can get away with clicking next on the screens you're presented with.

Creating the JSON Class

To create a new class, go to your new project and open the src folder. Right click on the package, expand new and select class. Make sure to name it JSONParser.

The class itself is a fairly generic and widely used one. It makes a post request to the provided URL and converts the response to JSON format.

Note: It's normally the best practice to have your text in the strings.xml file.

Main Activity

Now for the main activity, which is in Src → Packagename → MainActicity.java...

Again, this is fairly simple. We have a method called getAge, which is called when the button is clicked. It passes paramaters such as URL and name over to the JSONParser and creates a JSON object from the response. This is then read and either the age or an error message is displayed in the results text view depending on whether the query was a success or not.

Conclusion

Yes, the end result is extremely underwhelming. We've made a number appear on the screen. We have actually done much more than that, though. We've taken information from one platform and sent it to a completely foreign platform. The foreign platform was able to perform a task based on this information and to retrieve information from a database which the original platform had no knowledge of or possible access to. The second platform was able to send information back to the original which was then able to display the information to the user.

When it's put like that, it sounds pretty cool and infinitely useful! You can see how such techniques allow multiple platforms to share data, and how different data sources can be combined.

Simple text objects such as JSON are very similar to morse code. They transcend other languages and can be universally understood. This makes them extremely useful and versatile. It's the simple things that often solve the complex problems.

Leif Johnson has had well-rounded career in IT, which has covered many job roles such as DBA, Network Engineer, IT Manager and currently a Cloud Engineer. Despite the varying job titles, Web and Android development have remained a constant, with a project always being maintained or worked.