Browse DevX
Sign up for e-mail newsletters from DevX


Turn Your J2ME Mobile Devices into Web Service Clients : Page 5

If Web services are expanding within your enterprise, it might be time for you to look at all those mobile devices as potential clients of your Web services. Learn how to use the J2ME Web Service APIs to extend the enterprise to include J2ME devices with a simple example application.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Parsing the XML Using JAXP
Once the Web service has been called and the J2ME application has received the XML document (in the form of a string), the MIDlet will take advantage of the second Web service API, the JAXP API, to parse the XML and offer the life insurance client some information about policies.

Most of the parsing work in the sample application is done in the parseQuote method of the MIDlet. This method is called by the run method and is sent the string received in the Web service. It sends the XML string to a SAX parser and Handler to extract the element data out of the XML document into POJOs (plain old Java objects—in this case InsuranceQuote Java objects).

An instance of com.roaminginsurer.QuoteParserHandler (which inherits from org.xml.sax.helpers.DefaultHandler) were created with the MIDlet initialization in the startApp method.

parserHandler = new QuoteParserHandler(); try { SAXParserFactory factory = SAXParserFactory.newInstance(); saxParser = factory.newSAXParser(); } catch (Exception e) {

The SAXParser and superclass DefaultHandler are both from the JAXP API. When the XML document is received the parser and handler are engaged to extract data from the document.

parserHandler.reset(); saxParser.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")), parserHandler);

The SAXParser (SAX stands for Simple API for XML Parsing) reads the XML document and detects the beginning and end of the document, start and end of each element in the document, and so on. As it detects or parses the parts of the document, it calls on the associated handler instance to do something with the information located.

As seen in Listing 6, the QuoteParserHandler overrides the DefaultHandler's startElement method. As the SAXParse detects the start of each new element (in this case the start of each new policy quote) it puts the element's attribute information into a plain old Java object (POJO).

The POJOs are collected and then passed on to the displayQuote method where the policies are listed in the List screen and the details are shown in an Alert screen when requested by the user.


Figure 7. The Final Product: The GetQuoteMIDlet can be tested through any of the emulators provided with the Wireless Toolkit.

Building and Running the MIDlet
All that is left to do is to build and test your MIDlet. The Wireless Toolkit makes this easy. Simply push the Build button on the tool bar, fix any compile errors, and then push the Run button to execute your MIDlet in the emulator selected. Make sure your Web service in Tomcat is still up and running so that it can respond to the J2ME client's request.

Limited Functionality
The J2ME Web services APIs (JAX-RPC and JAXP) are still subsets of the enterprise versions of these packages. J2ME devices, by nature, are limited and so too are the Web service features they can support. Below is a list of some of those limitations and features the current implementations do not support.

  • No support for service endpoints (no J2ME device based web services—only clients).
  • No service discovery support (UDDI).
  • Validating parsers are not required given memory and processor needs.
  • SOAP 1.1 encoding is not supported.
  • SOAP messages with attachments are not supported.
  • SOAP message handlers are not supported.
  • XSLT is not supported.
  • DOM is not supported.
  • No support for dynamic proxies or dynamic invocation interface (DII).
A simple search for "J2ME Web services issues" in Google will give you plenty of additional information about the shortcomings of this new set of J2ME APIs. The issues range from the size of the packages, to security, to handling loss of connectivity. You should also know that there are also some alternatives in open source side (kSOAP and kXML).

The calls for "more" and "better" are not unfamiliar in our industry, but don't let that overshadow what these new APIs can do. I view these calls as harbingers that Web services for J2ME devices are here to stay and coming soon to expand your enterprise systems.

Jim White is a senior technical architect with Target Corporation. He is also co-author of Java 2 Micro Edition, (Manning).
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date