Life-time learner's blog

Java REST client for Android

In one of my previous posts I told you that I started learning how to code for Android, and created my first Android app. But most of mobile apps are useless when they do not exploit the advantages of the Internet. That is why in order to create a good application you should know how to use the power of the Internet in your app.

There are different ways of how your application is interacting with the Internet and its resources, for instance an app can download data, connect to remote databases, but the most popular way of interaction is RESTful services  (in the development of mobile apps). It just works more effectively than SOAP when the bandwidth is limited.

In this article I’ll show briefly how to create a client for a RESTful service in Android (this should also work in any device supporting Java).

There are a lot of libraries for working with RESTful services. Among the libraries suggested on StackOverflow I chose CRest.

According to the website,  CRest (Client Representational State Transfer or Client REST) is a lightweight JAX-RS compatible framework aiming to simplify the integration of external REST services into java applications.

The library is developed by CodeGist and you can find sources and binaries on GitHub repository:

CRest allows the developer to focus on the essential aspects of the integration of a REST service, such as the definition of:

  • the java interface that reflects the remote REST API.
  • the data model the interface will deal with.

And this is totally true! The library is awesome!

Using CRest is as simple as a piece of cake:

// Get a CRest instance
CRest crest = CRest.getInstance();

// Get a StatusService instance
StatusService statusService =;

Here, MyService is an interface for your service remote API. It uses special annotations to configure the service.

public interface MyService {
    // example of the post method
    Status updateStatus(
            @FormParam("status") String status, // this will be inside request content
           @QueryParam("lat") float lat, // this will be in the query (as in GET)
           @QueryParam("long") float longitude);

    // uses GET method for{id}/user_info
    User getUserInfo(@PathParam("id") long userId);

   // uses GET method for{userId}
    User[] getFriends(@QueryParam("user_id") long userId);

Actually, if you the service itself is written using JAX-RS, the interface of the client will look almost the same as the interface of the service, even annotations are the same.

The service returns data in JSON format and CRest automatically converts it to a Java object. Very convenient!

As of  March, 23, when this article was written, the latest version of CRest was 2.0.0-RC1. Unfortunately, due to some dependency problem, it doesn’t seem to work on Android. You have to provide jackson library to get it to work properly.

Download jackson-all-1.9.0.jar and add it to the list of libraries of your project.

Include CRest and jackson libraries to your project

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: