Prototyping with Mobile Phones – Running Restlet on Android

For prototyping purposes I had to develop a communication mechanism between a mobile phone and computer. I did not want to rely on push services like Google Cloud Messaging [1] as I simply had to push information to a single Android device and I wanted to have full control over the data and communication. Therefore, a client-server model seemed to be appropriate.

As I am addicted to REST, its Java EE integration and lightweight applications I was looking for a component which promises a webserver with REST capabilities and corresponding annotations. I have not to look around for a long time to find Restlet [2] which is a framework that can be used in (more or less) all Java applications. Restlet provides its own hosting capabilities, so that it is easy to start and run a webserver on an Android device and to use Java annotations to create a Restful API.

You only need a few stops to create your own server. In the example below an interface is created, which allows to add several books to a database via a Http Post and XML.

1 – Get the right Restlet Jar

Download Restlet for Android and put it into the build path of your Android application.

2 – Create an interface for the service you like to host

public interface IBookService{
@Post
public String updateBooks(Representation entity);
}

3 – Implement the interface

public class BookService extends ServerResource implements IBookService {
@Override
@Post
public String updateBooks(Representation entity) {
 
// Do the XML parsing
 
String result = "outcome"; // for testing I returned an "ok" or an the exception
 
return result;
}
}

4 – Create a RestletApplication so that you can attach your service to the restlet server

public class BooksRestletApplication extends Application {
 
@Override
public synchronized Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/books", BookService.class);
 
return router;
}
}

5 – Start your services

I started the interface in the onCreate() method of the Android app.

try {
component = new Component();
component.getServers().add(Protocol.HTTP, 8080);
component.getDefaultHost().attach("/testingrestlet",
new BookServiceRestletApplication());
 
component.start();
} catch (Exception e) {
System.out.println("!!! Could not start restlet based server");
e.printStackTrace();
}

Additionally, I used an observer pattern to update data and UI each time a post was received.

The implementation is straightforward and easily done, if you have used REST before.

Running webservers and especially REST interfaces on mobile phones seem to me to have high potential for a variety of application scenarios. I can imagine applications ranging from smart home appliances (have also a look at Albrecht’s and my IEEE article [3]), cross platform development for phones to access the phone’s sensor data to a lot of IoT scenarios. As soon as IPv6 is common and each phone has its own IP address such an approach could be very powerful. However, in the meantime we have to be satisfied with wireless network configurations.

[1] Google Cloud Messaging, http://developer.android.com/google/gcm/index.html
[2] RESTlet, http://www.restlet.org/
[3] Schmidt, A.; Bial, D.; , “Phones and MP3 Players as the Core Component in Future Appliances,” Pervasive Computing, IEEE , vol.10, no.2, pp.8-11, April-June 2011