Spring Android: Bringing Spring Components to Your Android Apps
Android has grown rapidly in recent years, increasingly challenging the iPhone in units sold and apps available. One major reason for the strong app presence on Android is its Java programming API, which allows the large number of Java developers to bring their existing skills from the server or the desktop to the mobile device.
However, bringing over your skills and bringing over your code are two different things. Most applications written today, particularly on enterprise servers, use popular frameworks that run on Java but are not included in the core Java language. If you wish to bring such an app to Android, you will need to replace all your app's calls to the framework, which could take as long as simply rewriting the app from scratch.
Help is on the way. SpringSource, a company that supports Spring and several other popular open-source projects, has recently announced the creation of Spring Android. This is not a complete reimplementation of Spring, but rather a port of some of the most useful client-side components of Spring to Android. Their initial release includes a port of commons logging, which is internally used by a wide variety of projects, as well as RestTemplate, which significantly eases the development of RESTful client applications.
This article will show how you can add Spring Android to your app, and walk through an example of using it to quickly build a Twitter client.
Things You'll Need
The Spring Framework and REST
Spring has become one of the most popular Java frameworks, offering a variety of useful services for applications. While it can be used on many platforms, it has gained its greatest popularity on enterprise servers, where it offers useful, intuitive, and scalable mechanisms for managing data and handling client interactions. Spring contains a number of modules, including several aimed primarily at client applications. One of Spring's strengths is its ability to abstract away the complexity of network interactions and provide a more programmatic way to communicate.
Representational State Transfer, or REST, is a type of software architecture that uses verbs and nouns to govern all client-server interactions. Based on HTTP, it supports commands to add data (via
PUT), view data (via
GET), modify data (via
POST), and so on. Each URI represents a conceptual noun, so a command to
GET http://example.com/cake.dat refers to the same entity that you would remove with a command to
DELETE http://example.com/cake.dat. Because REST does not rely on the server maintaining the client's state, it tends to be much simpler and more scalable than other Web architectures.
The key piece that Spring Framework offers to support RESTful clients is RestTemplate. This class supports generating URIs at runtime that refer to REST resources. It also provides a pluggable framework for translating the server's responses from raw HTTP into a form usable by your application. Out of the box it can translate basic string and binary responses; with popular third-party plug-ins it can parse standard response types such as JSON; and, with a bit of work, you can write your own custom converter to handle your server's responses. For this example we'll be using the Jackson library to parse JSON responses from Twitter; however, if you already have your own RestTemplate-based app, you can use your own message converter instead.
Adding Spring Android to Your Project
If you don't already have a working Android environment on your box, follow the links above to install recent versions of Eclipse, Android, and the Plug-in. Spring Android should work on all recent versions of the Android SDK; this article was written using version 2.2. You should also download the JAR files for Spring Android and Jackson.
Once your Android environment is working properly, create a new Android project called Springy. Figure 1 shows the options you can place in the wizard to create your skeleton project.
Figure 1. Creating a New Android Project in Eclipse
Since we'll be accessing the Internet to retrieve Twitter data, open AndroidManifest.xml and add a uses-permission directive for android.permission.INTERNET, as Figure 2 shows.
Figure 2. Adding Internet Permission to the Android Manifest
You'll also want to make a slight edit to the auto-generated file in /res/layout/main.xml. Add the
android:id attribute to the TextView.
This will allow us to reference the text view from within our app.
Figure 3 shows how to add the necessary JAR files to your project's Java build path. Do not add the commons logging JAR; it will automatically be included as needed. We're including the Jackson jars here so Spring Android can automatically parse JSON responses from Twitter. If you have your own message converters, you can add the appropriate JARs here or import those .java files into your code.
Figure 3. Adding Spring and Jackson JAR Files to the Android Project