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 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 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 = "{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,

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

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

  .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

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


Spring-AndroidAndroidgeonamesAndroid Development with Maven

Leave a Comment

Scroll to Top