Mon

04

Jul

2011

Consuming RESTful web services on Android using Spring Android

In order to simplify the development of native Android applications, the Spring community produced Spring Android, an extension of the Spring Framework specifically targeted to Android. This extension provides a Representational State Transfer (REST) client for Android as well as authentication support for accessing secure APIs. In this blog post, we are going to see how to use Spring Android to write client code for an Android application that consumes RESTful web services.

Let's suppose that we have a web service that handles HTTP GET requests to get all the currently available products, each described by an id, a name and a price. The implementation of the Product class is found in the listing below:

 

 public class Product {
private long id;
private String name;
private double price;

// the usual getters and setters...
}

 

And, assuming that the web service serves JavaScript Object Notation (JSON), the response entity-body for a GET request for all available products might look as follows:

 

[{"name":"Banana","id":1,"price":23.0},{"name":"Peanut butter","id":2,"price":50.5}]

 

Now that we have looked at the REST service, let's see the steps involved to consume it in an Android application using Spring Android's RestTemplate.

  1. Start a new Android project named HelloSpringAndroid.
  2. Now add the required Spring Android's JARs (i.e., spring-android-core-1.0.0.M3.jar, spring-android-rest-template-1.0.0.M3.jar) and their dependent JARs (i.e., jackson-core-asl-1.8.2.jar, jackson-mapper-asl-1.8.2.jar) to the libs directory of your project and include them in the project's classpath. You can download the JAR files from http://www.springsource.com/download/community?project=Spring%20Android and http://wiki.fasterxml.com/JacksonDownload.
  3. You also need access to the Internet in order to connect to the web service, so you must also request the INTERNET permission. In the Android manifest file AndroidManifest.xml, add the following as a child of the <manifest> element:

     

    <uses-permission android:name="android.permission.INTERNET" />
    

     

  4. Open the HelloSpringAndroid.java file and add the onCreate() callback method to the class:

     

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    

     

    In order to connect to the RESTful web service described above and fetch the results, you need to add the following lines of code at the end of the onCreate() method:

     

    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate();

    // The URL for making the GET request final String url = "http://10.0.2.2:8080/rest/products"; // Instantiate the HTTP GET request, expecting an array of // Product objects in response Product[] products = restTemplate.getForObject(url, Product[].class);

     

  5. The response of the web service are JSON objects, so in order to convert the response into Java objects you need to create the Product.java file on the client as well (see the beginning of the blog post for its definition).
  6. That's all. (This post has only described how to perform a HTTP GET request to a RESTful web service; however, the other RESTful methods (i.e., POST, PUT and DELETE) can be implemented in a similar manner.)

 

 

Write a comment

Comments: 0

  • loading



StatCounter
About | Privacy Policy | Print Version | Sitemap | Recommend this site!
© Matthias Braunhofer. All rights reserved