Академический Документы
Профессиональный Документы
Культура Документы
Article
Introduction to J2ME Web Services
Print-friendly Version
By C. Enrique Ortiz
April 2004
Developed within the Java Community Process as JSR 172, the J2ME Web Services
API (WSA) extends the Java 2 Platform, Micro Edition to support web services. The
API's two optional packages standardize two areas of functionality that are crucial to
clients of web services: remote service invocation and XML parsing. This article
introduces these two packages, shows you how to use them, and describes the support
for them in the J2ME Wireless Toolkit.
To learn more about the complexity of developing wireless applications in general,
please see the article "The Complexity of Developing Mobile Networked Data Services,
J2ME Wireless Connection Wizard For Sun ONE Studio," which describes the main
elements of mobile networked applications.
Introduction
WSA is designed to work with J2ME profiles based on either the Connected Device
Configuration (CDC) or the Connected Limited Device Configuration (CLDC 1.o or
CLDC 1.1). The remote invocation API is based on a strict subset of J2SE's Java API for
XML-Based RPC (JAX-RPC 1.1), with some Remote Method Invocation (RMI) classes
included to satisfy JAX-RPC dependencies. The XML-parsing API is based on a strict
subset of the Simple API for XML, version 2 (SAX2).
The goal of WSA is to integrate fundamental support for web services invocation and
XML parsing into the device's runtime environment, so developers don't have to embed
such functionality in each application -- an especially expensive proposition in resource-
constrained devices like mobile phones and personal digital assistants.
Core Specifications
The core specifications and application-level protocols that define web services are
promoted by the Web Services Interoperability Organization (WS-I), and governed by
the World Wide Web Consortium (W3C) and the Organization for the Advancement of
Structured Information Standards (OASIS). Four key standards specify how to create,
deploy, find, and use web services:
Web Services Standard Description
Simple Object Access Protocol (SOAP) 1.1 Defines transport and data encoding
Web Services Definition Language Defines how remote services are described
(WSDL) 1.1
Universal Description, Discovery, & Defines how remote services are discovered
Integration (UDDI) 2.0
Extensible Markup Language (XML) 1.0, Defines the Extensible Markup Language
and XML Schema (XML) and XML Schema
These primary specifications tend to be very broad, and developers of web services have
found it difficult to achieve full interoperability. To resolve differences in interpretations
of the standards, WS-I has defined a set of conformance rules called the WS-I Basic
Profile, version 1.0. JSR 172 conforms to the Basic Profile.
Other than exceptions such as RemoteException, stubs use most of the above API; the
application itself uses the stub.
// MIDP
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
...
// JAX-RPC
import java.rmi.RemoteException;
// JAX-RPC
String serviceURL = "www.j2medeveloper.com/pubwebservice";
String articleID = "IntroJSR172";
...
try {
// Invoke the PubService method getArticleByID() to retrieve
// a specific article by ID. A WirelessArticle represents an
// article and has methods to get the article's author, date,
// title, summary, and body. PubService also has a method to
// get the PubService's RSS feed.
WirelessArticle article = service.getArticleByID(articleID);
// Create a Form to display the article.
javax.microedition.lcdui.Form form =
new Form(article.getShortTitle());
form.append(wrap(article.getSummary()));
...
...
display.setCurrent(form);
} catch (RemoteException e) {
// Handle RMI exception.
} catch (Exception e) {
// Handle exception.
}
...
Using stubs makes remote service invocation very easy. This code instantiates
PubService_Stub, initializes the instance, and invokes one of its methods,
getArticleByID() to retrieve the article itself. The application then uses the article's
access methods to get the article's title, summary, and so on.
Note that invoking PubService_Stub.getArticleByID() is a blocking call, so in a real
application you would dispatch the call in a separate thread. In WSA, method invocation
follows the familiar synchronous request-response model of client-server interaction: the
client makes a request to the server, and then waits for a response:
Remember that CLDC 1.0 does not support floating-point types, so float and double
must be mapped to String. Developers should use this default mapping when targets
include both CLDC 1.0 and CLDC 1.1 platforms.
For more information about WSDL, please refer to the JSR 172 specification, and
W3C's WSDL specification.
The following method definition shows how to use JAXP to parse an RSS feed retrieved
from PubService:
/**
* Invoke the service and parse the RSS feed.
*/
private void parseRSSfeed() {
try {
// Create a RSS parser handler
parser = new RSSParserHandler();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
saxParser = factory.newSAXParser();
} catch (Exception e) {
// Handle exception...
return;
}
if ("item".equals(qName)) {
title = link = description = null;
}
stack.push(qName);
}
Summary
The J2ME Web Services Specification standardizes programming interfaces for web
services and XML parsing in the J2ME platform. With the advent of the JAX-RPC
subset API, developers can use the Java programming language and familiar JAX-RPC
APIs to create applications that consume XML-based remote services, without having to
deal with the low-level details of HTTP, SOAP, and data marshaling. And with the
introduction of the JAXP subset API, XML parsing is now part of the J2ME platform
itself. The J2ME Web Services API eliminates the need for developers to include code
for remote invocation and XML parsing in each application.
These powerful APIs give mobile applications much easier access to web-based
services, but developers must keep in mind the restricted application environment J2ME
devices provide. Simply porting existing XML-based desktop or enterprise applications
is not the proper way to develop web services clients for the J2ME platform. Due
consideration of device processing power, battery life, network bandwidth, and security
is essential.
The Complexity of Developing Mobile Networked Data
Services, J2ME Wireless Connection Wizard For Sun ONE
Studio
These advanced wireless applications are not standalone, however. They are part of a
complex structure that spans wireless devices, wireless networks, the Internet, and back-
end systems that typically reside on enterprise platforms.
Creating networked wireless applications requires a broad set of skills, and detailed
knowledge of client technologies like the Mobile Information Device Profile (MIDP), a
fundamental part of Java 2 Platform, Micro Edition (J2ME), of server technologies like
servlets and web containers, and of the mechanisms they use to communicate with each
other.
The J2ME Wireless Connection Wizard for Sun ONE Studio facilitates the creation of
networked wireless applications and services by automating significant parts of the
development process.
To see how the Connection Wizard can help you, you must first understand what is
typically involved in the creation of networked wireless applications.
A closer look at the client device and server components is shown in Figure 2.
This figure reveals an intricate set of components, indicating that the creation of a
networked wireless application is a complex problem.
The Wireless (Client) Device and Application
On the wireless device we have the networked application, typically based on the J2ME
platform's MIDP. The typical application structure may consist of the application logic,
which drives the user interface and overall application behavior, and a remote service
invocation and network layer that encodes requests to and and parses responses from a
server on the Internet. The application may also store data locally, on the device. Figure 3
provides a high-level summary of a typical wireless client application.
Creating the network logic requires developers to be familiar with how HTTP works,
which services are available and their location, and how to encode requests and decode
responses. This task can be cumbersome and must be repeated for each new application.
To avoid repetition some developers may create a library of helper classes, but this can be
bulky and complex.
To invoke remote services on a server on the Internet, some developers rely on their own
application-level network protocols or XML dialects, while other developers may use
XML-RPC and SOAP, both discussed soon. Standards such as Web Services for J2ME
promise to standardize how clients consume remote services, but these are still emerging.
Because creating the application structure and implementing the network logic are very
similar from one application to the next, both tasks are excellent candidates for
automation - which is where the J2ME Wireless Connection Wizard for Sun ONE Studio
becomes extremely useful.
The Server and Back-End Systems
At the other end of the networked wireless application are the server and back-end
systems, which typically are based on the Java 2 Platform, Enterprise Edition (J2EE). The
server has the actual published services that are available to the client.
As Figure 4 shows, the structure of the server side application typically consists of a web
server running a servlet and JavaServer Pages (JSP) engine, the server-side Java Servlet
or JSP that processes incoming service requests and generate responses, supporting
application classes, and back-end resources such as Enterprise JavaBeanscomponents
(EJB and databases.
Creating the server side can also be an elaborate and cumbersome process that requires a
broad set of skills. As in the case of the client application, creating the server application
entails a number of steps that must be repeated for each new application. These include
determining the structure of the Servlet, how to read requests and write responses, how to
expose services (increasingly, web services), and how to abstract the back-end services.
All these repetitive tasks are also excellent candidates for automation by the J2ME
Wireless Connection Wizard.
What varies the most among the various service-request mechanisms such as XML-RPC
and SOAP is the way the data is encoded and packaged. XML-RPC, the precursor of
SOAP, provides a simpler approach for XML-based remote procedure calls, but SOAP is
more in line with the defined standards for web services.
Because remote service invocation is not yet standard on J2ME platforms, to use remote
services developers typically rely on their own solutions, or in third-party solutions such
as kXML-RPC and kSOAP. And because both XML-RPC and SOAP are XML-based,
they require an XML parser in both the client and the server, and the corresponding
supporting classes. XML parsers are also not yet standard on J2ME platforms, and
developers must embed parsers within their applications. Some XML parsers that can be
used in J2ME platforms include:
The J2ME Wireless Connection Wizard adopts this approach as well. To reduce demands
on network bandwidth - and save users time and per-byte charges - the Wizard transfers
raw data. This capability requires the addition of a framework to each application, but it's
very lightweight. At as little as 3 kilobytes, it consumes less memory than XML-based
remote service invocation and XML parsers, or protocols you create yourself, leaving
more room for your application. Note that this lightweight framework does not introduce
any new or proprietary APIs - the generated code is based on the standard MIDP APIs.
Testing
Testing is another important factor to consider when creating networked wireless
applications. Without good development and testing tools development becomes more
difficult.
The ideal development environment is integrated, allowing you to create, execute, and
debug networked wireless applications in a single development environment - another
benefit of the J2ME Wireless Connection Wizard, which can be integrated with Sun ONE
Studio and the J2ME Wireless Toolkit, as well as with emulators from major device
manufacturers.
The J2ME Wireless Connection Wizard makes two new templates available when you
select the New File Wizard from the menu bar in Sun ONE Studio (choose File -> New):
Figure 6: J2ME Wireless Connection Wizard Templates
(Click image to enlarge.)
By selecting Skeleton Application you can create simple versions of the client and
server applications that you can use as the base for your networked application. Or you
can select the Connection Mapping wizard seen in Figure 7, which walks you through a
series of steps, allowing you to choose the services that you want to export, and the target
client. The wizard then automatically generates the client and server communication
components for you.
From there you can select any method to export (or remove unnecessary ones). The
wizard will automatically update both the client and server skeletons and components for
you.
Remember that this support adds only a small amount of network code to your
application, uses HTTP as the transport, and exchanges raw data, keeping the volume of
data exchanged to a minimum.
Because the J2ME Wireless Connection Wizard is integrated with Sun ONE Studio and
the J2ME Wireless Toolkit, you can easily use their debugging capabilities to test your
application:
Figure 9: The J2ME Wireless Connection Wizard Integrated with Sun ONE Studio
(Click image to enlarge.)
The Wizard comes with documentation and sample code. For more information please
see the J2ME Wireless Connection Wizard page.
Conclusion
This article has covered the typical elements of a networked wireless application.
Creating an end-to-end wireless application can be a cumbersome task, and requires a
broad set of skills. The J2ME Wireless Connection Wizard for Sun ONE Studio
simplifies the creation of networked wireless applications, by automatically generating
J2ME and J2EE code for you. You can concentrate on your application logic, and rely on
the Wizard to handle the communications aspects of your applications.
Acknowledgments
We want to recognize the help given for this article by James Allen Senior Product
Manager, Sun ONE Studio, Mobile Edition, Sun Microsystems, Inc. James is the product
manager for the J2ME Wireless Toolkit and Sun ONE Studio, Mobile Edition developer
products. He manages the mobile developer tools strategy at Sun Microsystems, Inc.
About the Author: C. Enrique Ortiz, Director of Engineering, Mobile Applications for Aligo. He is an
active participant in the wireless Java community, and is the co-author of the Mobile Information Device
Profile for J2ME published by John Wiley and Sons. Enrique holds a B.S. in Computer Science from the
University of Puerto Rico and has over 13 years of industry experience.