Connecting to a Cloud Service (Google App Engine) through Android
Google's Android is the Mobile OS platform targeted towards cell phones, smartphones, and tablets. With exploding growth in the number of portable devices like smartphones and tablets, many bill mobile computing as the next major phase in computing and communications. Released just a couple of years back, Android has went on to capture a major share of the mobile phone market unsettling rivals like Symbian OS, Windows Mobile OS, and others. According to a recent report, Google activates more than 1,00,000 Android devices each day and the trend is ever increasing.
Among other factors such as Java based environment, lightweight kernel optimized for mobile platforms, good UI, and great security features, strength behind widespread adoption of Android lies in the fact that it is a wonderful Open Source software which allows developers to exploit full capabilities of the OS in order to develop applications which are limited in nature and scope only by the imaginations and creativity of the developers. However, there is a caveat. No matter how good a platform is, it is always limited in its performance by the hardware on which it runs, and although the muscle power that is packed in smartphones and other mobile devices of today is approaching that of a low-end laptop, there are still limitations on how much you can do on a mobile phone.
In order to make an OS platform truly encompassing and next-gen, it is always desired that the OS platform incorporates features which would allow users and developers to easily and seamlessly communicate with other devices and portals on the Internet so that those features can collectively increase manifold the capabilities that can be achieved. With the advent of Cloud Computing, this desire has gradually become a necessity. Google has been pretty successful in doing so by allowing network communication pretty simple and straightforward process. In many cases, there are APIs available that take care of the communications, in others developers can utilize simple HTTP protocols to communicate.
Since Google itself focuses heavily on Cloud Computing and already has a robust Cloud infrastructure in place through its various product offerings, it would be natural for Google to offer features in Android that can leverage the Cloud capabilities. Imagine an inventory app for a company which needs to take data from hundreds of warehouses spread across the globe, and then needs to predict the inventory levels for multiple products at multiple locations. In addition, wouldn't it be helpful for inventory managers, suppliers and other stakeholders to be connected to this humongous data warehouse through their mobile phones so that they can in real-time check various statuses and can then adjust inventories of various products based on that intelligent info thus resulting in substantial cost savings and leaner operations for the company. This is just a possibility which can be achieved through Android and Google App Engine.
In this entry, I seek to describe the process of connecting from Android to a Notes app in Google App Engine. Once the process is understood, it can then be replicated with minor modifications for various applications. I have created and hosted a Notes application on Google App Engine which lets users log-in with their Google credentials and then add, update, and delete To-Do notes for themselves. We want to increase this functionality on Android.
For this we create an application on Android which consists of two simple activities. On the first activity, we list all the accounts for which a user wants to connect with the Notes app. You can add accounts in Android easily and can add as much accounts as one wants. Once a user selects a particular account, Android app will then attempt to establish a communication channel with the Notes app on App Engine using the simple HTTP calls. You need to make sure that you have set permissions for the Android app to use CREDENTIALS and INTERNET.
The Android app will take user credentials related to that account from the Accounts and will get an Authentication Token. An AuthToken is the token which you get when you log-in to the App Engine app using the credentials. We get auth token by creating an instance of AccountManager and then by calling getAuthToken method on it. Once we have the auth token, we then trade it with an authentication cookie, which is then used in all subsequent communications with Notes app. For this we first create a HTTPGet method which contains the URL of the Notes app in the specified format.
HttpGet http_get = new HttpGet("https://infytodo.appspot.com/_ah/login?continue=http://localhost/&auth=" + tokens);
We then create an instance of HttpClient and call on it the method execute, which will have the HttpGet method as parameter. The response object which we obtain contains the Cookie. We call the method getCookieStore().getCookies() on httpClient object to obtain the auth cookie.
Once we have the cookie, we then execute requests for getting the notes and uploading the notes using execute method of HttpClient object which contains the URL of the Notes app as parameter.
Whenever we request for the notes of a user, it gives us the response in a number of formats, an InputStream or a JSON object are few of the examples. We can use other formats such as SOAP as well to retrieve the response. Here, we are getting the response in form of JSON objects. For this we have added a piece of code in the Notes app on App Engine which formats and gives response in form of JSON object. At the Android app end, when we get the JSON object, we then decode it using the gson library which is provided by Google and then display the To-Do notes of a particular user as a series of Text strings in a new activity window.
In addition to retrieving the notes, a user can also add notes using the same procedure. The only difference in the end will be to call the appropriate URL which is exposed by the App Engine app in order to add the notes. Once the request is successful, a status message is thrown back stating that the operation has been successful.
This entry gives a glimpse of the powerful yet easy capabilities of the Android OS to communicate with the Internet in general and App Engine in particular. Developers can exploit these capabilities in a number of ways in order to make their Android apps much more capable, useful, and informative.