A nearshore team from Uruguay, South America (GMT-3) Accessing oData from Android using Restlet - UruIT Blog
Tuesday, September 13, 2011 11:48 PM uruit

Accessing oData from Android using Restlet

Hi! In this post we will see how to consume oData from Android. oData is an open web protocol for querying and updating data that has become very popular lately due to it’s simplicity and availability of tools and libraries.

1. To consume oData from an Android application we will use a library called “Restlet”. It can be downloaded from here: http://www.restlet.org.
Note: We will use the JRE Restlet libraries and the Android Restlet libraries so you will have to download both.

2. Once you downloaded both libraries the first step is to create a Java program to auto-generate the entities and the proxy service that will be used to consume oData from our Android application. The Restlet libraries can automatically generate the entity classes to access the oData service but that functionality isn’t available in the Android Restlet libraries, so we first need to generate a Java project and use the JSE Restlet libraries to generate the entities.

3. So, create a new Java project within Eclipse and add the external JSE jars.

Untitled

This is done by right clicking on the project -> Build Path -> Configure Build Path. Click on Add External JARS.. and go to the folder where Restlet was installed (by default C:\Program Files (x86)\Restlet Framework\Edition Java SE\2.0.4\lib). Add the following libraries:

  • org.restlet.jar
  • org.restlet.ext.odata.jar
  • org.restlet.ext.freemarker.jar
  • org.restlet.ext.atom.jar
  • org.restlet.ext.xml.jar
  • org.freemarker_2.3\org.freemarker.jar

Untitled

4. Then create a Main class and add the following code:

Generator.main(new String[] {
"[service url]",
"[folder path]"
});
  • [service url] – Url of the oData service
  • [folder path] – Folder path where the Generator will copy the auto-generated classes

Untitled

5. Run the project and go to the [folder path], you will see a class in the root of the folder (this is the proxy service) and a folder in the root with all the entities of the service within.

6. After that we have the proxy and the entities ready to be used from our Android App.

7. Import classes to our Android Project: Right click on the src folder, Import->File System and select the [folder path].

Untitled

8. How to use the auto-generated classes?

a. First of all we need to reference the Android Restlet jars (by default C:\Program Files (x86)\Restlet Framework\Restlet Framework\Edition Android\2.0.4\lib). Add these libraries:

  • org.restlet.jar
  • org.restlet.ext.odata.jar
  • org.restlet.ext.atom.jar
  • org.restlet.ext.xml.jar
  • org.restlet.ext.net.jar

b. After creating an instance of the proxy, you are ready to call any method you want:

IDataServiceProxy proxy = new DataServiceAtomPubProxyImpl(DataServiceClient.URL);

9. That’s it! Here is a screen capture of our Android App consuming oData:

Untitled

 

Now we will see some examples that show how to work with the proxy we just created.

 

The following example gets all the café entities and displays some of their properties:

TestAssociationOneToOneService service = new TestAssociationOneToOneService();
Query<Cafe> query = service.createCafeQuery("/Cafes");
for (Cafe Cafe : query) {
System.out.println(“id: ” + Cafe.getID());
System.out.println(“name: ” + Cafe.getName());
}

The following example gets a single entity and displays some of its properties:

Query<Cafe> query = service.createCafeQuery("/Cafes('1')");
Cafe Cafe = query.iterator().next();
System.out.println(“id: ” + Cafe.getID());
System.out.println(“name: ” + Cafe.getName());

Add a new entity:

Cafe Cafe = new Cafe();
Cafe.setID("3");
Cafe.setZipCode(12345);
Cafe.setName("Bar des sports");
Cafe.setCity("Paris");
service.addEntity(Cafe);

Delete an Entity:

Query<Cafe> query = service.createCafeQuery("/Cafes('1')");
Cafe Cafe = query.iterator().next();
service.deleteEntity(Cafe);

Get a single Entity and its associated entities:

Query<Cafe> query = service.createCafeQuery("/Cafes('1')").expand("Item");

 

Conclusions

We saw that is relatively easy to query and update data exposed by oData using the Restlet library. This article together with Consuming OData from iPhone and Consuming OData from Windows Phone7 conform our series of articles on how to consume oData from mobile applications. oData is definitely an important tool to consider when architecting multi platform service oriented applications.

 

I hope you liked the article. Thanks for reading!

Diego Acosta

Filed under: ,

Comments

# re: Accessing oData from Android using Restlet

Friday, September 16, 2011 8:04 AM by Aubrie

Great review, I love your post.  

<a href="http://www.zojirushibreadmachine.com">Buy Zojirushi Bread Machine zojirushibreadmachine.com</a>

# re: Accessing oData from Android using Restlet

Sunday, September 18, 2011 6:58 AM by Jerome Louvel

Hi Diego,

This is a great tutorial!

I have added a link to in in this list of community tutorials:

wiki.restlet.org/.../167-restlet.html

Best regards,

Jerome

# re: Accessing oData from Android using Restlet

Sunday, November 13, 2011 10:02 AM by Vivek

This is Brilliant!

Also like to know any other possibility of accessing it with using sybase unwired platform.

cheer's,

Vivek

# re: Accessing oData from Android using Restlet

Thursday, December 15, 2011 11:35 AM by duncnainnyGal

Haha that's rediculous. No way

# re: Accessing oData from Android using Restlet

Sunday, March 18, 2012 4:13 AM by Jeroen Custers

Hi Diego,

Nice tutorial. Do you know if it's possible to generate code for a rest service which requires basic authentication?

Thanks in advance!

Regards,

Jeroen

# Restlet createQuery fails with connection error (and after workaround NoSuchMethodError) | PHP Developer Resource

Pingback from  Restlet createQuery fails with connection error (and after workaround NoSuchMethodError) | PHP Developer Resource

# re: Accessing oData from Android using Restlet

Sunday, May 27, 2012 4:52 AM by cking24343

Ok, First of all this seems to be what I need. With that being said I'm a bit of a noob so forgive me if this question is a little bit ridiculous, but I can't seem to get past step 3. I keep getting the error "The method main(String[]) is undefined for the type Generator". Any suggestions?

Leave a Comment

(required) 
(required) 
(optional)
(required)