Web Services in BlackBerry J2ME Applications

by Edward Lineberry

12/30/2006

Introduction

The BlackBerry MDS Studio from Research in Motion provides a rich runtime and development environment for building applications and integrating web services on the BlackBerry. But the MDS environment may not be suitable for all situations. In this article, I'll show how to use web services in J2ME applications without the overhead of MDS Studio.

For an example application, I'll use a public web service from WebserviceX.NET that offers basic lnaguage translation functionality. Our input will be the language to translate from, the language to translate to, and the text to translate.

In order to focus on the details of web service implementation in J2ME, I'll skip over the basics of J2ME applications on the BlackBerry, which are covered in my first article, An Introduction to BlackBerry J2ME Applications, so please refer to that if you need a refresher.

Architecture

For this simple demonstration application, we will only create four classes: WebServiceDemo, WebServiceDemoScreen, ConnectionThread, and ResponseHandler. WebServiceDemo contains main. Just like in the DataCapture example of the first article, all it does is start up ConnectionThread and then load WebServiceDemoScreen. ResponseHandler is the SAX DefaultHandler that will parse the xml returned by our web service.

The user interface is pictured below. Two ObjectChoiceFields allow the user to select the languages, and a single input field captures the word to be translated. When the user activates the "Translate" button, an http GET connection is made to the web service, and the returned xml is parsed. In order to better illustrate the web service in action, we'll display both the raw xml, and the parsed result.

Connecting to the Web Service

In order to pass parameters to the web service, we need to pull the information from the user interface. The following code handles that task.

The parseResults method is responsible for displaying the single word translation taken from the xml. Again, in a real world environment, there would be no need to display the raw xml to the user. It's provided in this application in order to render the steps more transparent.

Xml Parsing

The BlackBerry API's make xml processing fairly trivial, and in the case of our simple web service, the xml returned is lends itself to an easy solution.

In this method, a SAXParser is created and our DefaultHandler and String are passed into the parse method. The only catch is we have to turn our String object back into a InputStream. In most situations, the xml would never be stored as a String but parsed instead directly from the Stream.

Finally, let's take a look at the ResponseHandler class. As the parser moves through the xml, events are triggered in the ResponseHandler.

The startElement method is triggered at the beginning of each element in the xml. It's only purpose is to store the name of the current element in the instance valiable element. The characters method extracts the data from inside the tag and places that into the public response variable.

In the following xml example, the text "servizi" would be extracted into the response variable.

Next Steps

In the example application, the input parameters for language are hard coded. If something in the web service changes, our application could break. Also, if new languages are added, our application will not be able to take advantage of that. The solution is to retrieve and parse the WSDL to extract this information. It's possible to build an entire dynamic user interface based on a web service's WSDL. For this demo application, the web sevice is trivial: two arguments in, one string returned. We can get by with a level of simplicity that would be impractical for most real world uses. More complex web services would require a more robust client application.

Recently Added

Recommendations

"STS has proven reliability with extraordinary competence in helping plan, develop and implement new technology in a variety of arenas. Their response is timely, their technicians are professional and I have never been disappointed with their service, they go beyond our expectations with each project." - Chief of Police L.D.Hobbs, Forest Park Police Dept