Tuesday, September 18, 2012

Opentravel Mobile Android App development with the JiBX open source framework

This example demonstrates how easy it is to create an Android app that can communicate with travel sites using the opentravel.org message schema.

The JiBX open source project is a java data-binding utility that simplifies handling of xml schema. The JiBX schema library includes pre-built bindings for many message schema, including the complete opentravel schema.

This app uses the 2011B version of the opentravel touractivities search request and reply.

Here is what this app looks like on my phone. Select a date and the available tour activities will display:
Tap on a tour for a detail display:


To illustrate what is happening, let's jump right into the actual source code. I create an opentravel xml touractivity search request and then read through the returned xml reply message.

// Populate the search request
SearchRQ searchRQ = new SearchRQ();
OTAPayloadStdAttributes payload = BaseService.createStandardPayload();
searchRQ.setOTAPayloadStdAttributes(payload);
DateTimePref dateTimePref = new DateTimePref();
searchRQ.setDateTimePref(dateTimePref);
dateTimePref.setStart(YMDdate);
dateTimePref.setEnd(YMDdate);

// Send it to the server and get the result
SearchRS searchRS = search(searchRQ);
if (searchRS == null)
return null;

// Extract the information from the results
List tourProducts = new Vector();
List tourActivities = searchRS.getTourActivityInfoList();
if ((tourActivities == null) || (searchRS.getErrors() != null))
return getErrorMessage(searchRS);
for (TourActivityInfo tourActivityInfo : tourActivities)
{
TourInfo tourInfo = new TourInfo();
TourActivityID tourActivityID = tourActivityInfo.getBasicInfo();
tourInfo.tourID = tourActivityID.getTourActivityID();
TourActivityDescription tourActivityDescription =  tourActivityInfo.getDescription();
if (tourActivityDescription != null)
tourInfo.description = tourActivityDescription.getShortDescription();
Pricing pricing = tourActivityInfo.getPricing();
if (pricing != null)
{
tourInfo.minPrice = pricing.getMinPrice();
tourInfo.maxPrice = pricing.getMaxPrice();
}
tourProducts.add(tourInfo);
}

// Return the tours to display
return tourProducts;

As you can see, manipulating the xml with java objects instead of xml makes the code easy to understand.

The rest of the code in this application handles the UI and the client calls. It should be familiar to any Android programmer. One of the nice features of Android is the inclusion of many popular open-source libraries, such as the Apache http Client library and the XMLPull parser.

This app is pretty simple. Setting up your development environment is a little tricky.

First, you will need a server that is set up to service your requests. Just follow the steps in my post here: http://blog.tourgeek.com/2012/04/creating-soap-and-rest-services-for.html . Make sure the server is working by pointing a browser to the server with this url (just replace this IP address with your server's IP): http://192.168.1.100:8181/cxf/touractivity/search/2012-09-04/2012-09-04 . You should get an XML reply message. Our app will be (html) posting a RESTful XML request rather than using url parameters.

Now we're ready to create our Android application.

Check out this example from github: https://github.com/jibx/schema-library/tree/master/schema-utilities/examples/android/org.jibx.android.demo.touractivity

I prefer maven for my Android development since it automatically includes all the correct opentravel binding modules automatically . You will need to set up maven for Android by following the directions here: http://www.sonatype.com/books/mvnref-book/reference/android-dev.html . Make sure you have deployed android 4.0_r3 into your local maven repo.

To build the code, just type:
mvn clean install

Now install your .apk file (from the maven target directory) onto your Android device.

Once you run the app, click the menu button to change the settings. Enter the IP address of your server and click okay:
You should see the tour display. Try changing the date to update the tour display (Some of the tours do not run on Tuesdays).

VoilĂ !

Take a look at the source code. You'll be amazed at how simple it is.

I hope this example helps you create your own app using the opentravel schema. If you have questions, contact me at don@tourgeek.com or place a comment in the space below.

Don