In the previous tutorials, I showed how to create Material Design android applications. Those applications were mostly UI based and didn’t really did an intensive background work. Switching views and showing some web service results is a trivial task which can be handled by the phone CPU easily. But what if we want to analyze our data before we serve it to our users? well, in that case, we would probably want to add some backend services to do all the hard work.
This tutorial shows how to add a Backend layer to your Android application and send/receive data from it
This Tutorial will Cover the Following:
- Adding a backend API Rest service module to your existing Android app
- Communicating with this API module over HTTP using Google Cloud Endpoints
Adding a Backend Module to Your Android App
Since I use the android studio, I found it very ease to go with google cloud platform. They provide all the needed APIs and services so at least for now it’s the perfect choice. Google cloud includes an App Engine server with Google Cloud Messaging (GCM) service.
Google Cloud Messaging (GCM) is a service that allows you to send push notifications from your server to your users’ Android devices, and also to receive messages from devices on the same connection. The GCM service handles all aspects of queueing of messages and delivery to the target Android application running on the target device.
Adding the module can be done easily by following this Google Cloud Platform, this provides your app with a Registration Endpoint. But what if we want our own Endpoint, we want a customized HTTP based REST service to manage our app calls. For this purpose we will create a new Endpoint, the following image illustrates the design concepts we follow.
After we added a built in backend module we want to add our own Rest API service, so our app client code will be able to communicate with the server. This is done by calling to HTTP requests (GET / POST) and transferring information using JSON. Currently, JSON is the only supported format, but it’s also the easiest and the best as far as I see it.
We start by creating a new class annotated with @API. The class public methods define the API REST methods, I defined 2 methods just to show how it’s done. Passing parameters into the API is very straight forward and done by adding Model objects into the method signature. The API automatically converts you object into a JSON format object.
Each method you want to be publicly available over your API, you need to annotate with @ApiMethod and specify the method type, HttpMethod.POST or HttpMethod.GET.
Passing parameters inside the URL is, of course, possible and you can read about it more at google’s documentation.
This is the API class
This is the returned value model class which we return from the Http calls
Communicating With The Backend
Before we will communicate with the service, we’re going to see it’s live and ready. Follow those steps to make sure your API is ready to serve requests:
- Start your backend module by pressing the play button
- Point your browser to http://localhost:8080/_ah/api/explorer – google exposes an admin panel to manage all our APIs to make sure we did it correctly.
- Go to the services section from the left menu and then select your API – I called it “process API”
- Inside the services section, you can see all the public methods which we annotated using @ApiMethod
- Click on one of the methods and you can see the exact path and parameters needed for this method, you can also execute the call and test the response.
So, we showed how to use Google Cloud Endpoints in order to generate a custom REST API for our mobile app, we also tested the API and tried to execute a few sample calls. The next step is to make our app call a method from this API and get the response.
The first step is to create a new class which extends the AsyncTask class, we then use the doInBackground method to execute our HTTP call in the background without holding the UI thread busy. Note that in order to import the API class into this class you must first trigger a rebuild for your server module. This class belongs to the client app code.
Finally, to trigger the async task just add this to your MainActivity.java file: