RESTFul client using Spring+Maven on Android

22 Jan 2011

RESTFul client using Spring+Maven on Android

Interested in diving into Restful client development on Android, but not exactly sure where to start then here is the article which can guide you to create Restful client on Android. This article assumes that reader of this article are familiar with Android development and also know about Restful service.Spring framework is very popular framework for j2ee and it come up with framework for mobile too.

In this Artcle, I used geonames.org Rest service to fetch the near places for a specific postal code which in radius.So first of we need to configure the project with maven. Please refer this to get the details of configuring maven for Android.

Your pom.xml file should look like this:

    src
    ${project.artifactId}

        com.jayway.maven.plugins.android.generation2
        maven-android-plugin
        ${maven-android-plugin-version}

${android.home}
${android-platform}

            Android2.1

          true
          true

        true

        maven-compiler-plugin
        ${maven-compiler-plugin-version}

After configuring the android project with maven, we are ready for some action. We need to connect to geonames.org to get the data from the rest service. Using spring platform for mobile, its very easy to do that. So here is the code which connects to service and fetch the results:

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

  // The HttpComponentsClientHttpRequestFactory uses the
  // org.apache.http package to make network requests
  restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());

  // The URL for making the GET request
  final String url = "http://ws.geonames.org/findNearbyPostalCodesJSON?postalcode={postalCode}&country={countryCode}&radius={radius}";

  // Initiate the HTTP GET request, expecting an array of State
  // objects in response
  PostalCodes result = restTemplate.getForObject(url,
      PostalCodes.class, this.postalCode, this.countryCode,
      this.radius);

In case restful service uses digest authentication then you can use this:

DefaultHttpClient client = new DefaultHttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("xxxx", "xxxx");

client.getCredentialsProvider()
  .setCredentials(new AuthScope("xxxx", 80, AuthScope.ANY_REALM), defaultcreds);
// The HttpComponentsClientHttpRequestFactory uses the
// org.apache.http package to make network requests
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(client));

The response from service returns json objects so we require pojo objects to populate the response and use them in android client. So we need to create two Pojo as

public class PostalCodes {
	private PostalCode[] postalCodes;
..
}
public class PostalCode {
	private String adminName2;
	private String adminCode2;
	private String adminCode1;
	private String postalCode;
	private String countryCode;
	private Double lng;
	private String placeName;
	private Double lat;
	private String adminName1;
...
}

So all code is in place and its time to build the android project using:

mvn clean install

We can start the emulator using

mvn android:emulator-start

We can deploy the application using

 mvn android:deploy

Home Screen for the android client would be:
Home Screen for android client

Results from Restful service:
Result Screen for android client

So we have created restful client for android without much efforts. Please feel free to send your comments/questions.

You can find the source code here

References:

Spring-AndroidAndroidgeonamesAndroid Development with Maven

  • http://www.javabeat.net Krishna

    Good article on learning the basic spring REST implementation: Spring REST Services

  • Valdemar Pereira

    Hi!
    I’m having a few issues running the example…

    It is throwing the following exception:

    ERROR/AndroidRuntime(18628): Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.alert.student.PostalCodes] and content type [application/json;charset=UTF-8]

    Is there something I’m missing here?

    Thanks in advance for any help you can provide. :)

    Cheers

  • jackyrong

    hi,your article is very good!Would you please send me the code of this article?thank you very much ,[email protected]

  • http://www.makeurownrules.com Kapil Jain

    Hi Valdemar/jackyrong,

    We are in a process to release this code so you can check the application yourself.

    Cheers,
    Kapil

  • Valdemar Pereira

    Thanks Kapil!! :)

  • http://www.makeurownrules.com Kapil Jain

    Hi Valdemar/Jackyrong,

    You can find the source code at http://code.google.com/p/geonames-android/

    Cheers,
    Kapil

  • Murali

    Hi,

    Can you please provide the details how to call a secured WebService from a stand alone java service(which was configured with spring core).I will appreciate if anyone has any experience on this and share with me.

  • http://www.makeurownrules.com Kapil Jain

    Hi Murali,
    I am not sure which webservice you are referring is it soap/rpc or rest ? I guess for all the services u should be able to bypass certificate validation using the same technique used in this article.

    Cheers,
    Kapil