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:

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:

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

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

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

We can start the emulator using

We can deploy the application using

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