Skip Headers
Oracle® Java Micro Edition Software Development Kit Developer's Guide
Release 3.3 for NetBeans on Windows
E24265-05
  Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
 
Next
Next
 

19 JSR 172: Web Services Support

The Oracle Java ME SDK emulator supports JSR 172, the J2ME Web Services Specification. JSR 172 provides APIs for accessing web services from mobile applications. It also includes an API for parsing XML documents.

See the following topics:

19.1 Generating Stub Files from WSDL Descriptors

The NetBeans IDE provides a stub generator that automates creating source code for accessing web services that conform to the J2ME Web Services Specification. You can add stubs to any MIDP application.


Note:

If you are using NetBeans 7.1.2, or 7.2.1 or higher the "SOAP Web Services" plugin must be installed and activated.


The following is a general procedure for adding stubs:

  1. In the Projects window, expand the tree for a project.

  2. Launch the Java ME Web Service Client wizard:

    Right-click the Source Packages node and select New > Other and select the CLDC category, then select Java ME Web Service Client....

  3. In the New Java ME Webservice Client page, you can do one of the following actions:

    • Click Running Web Service and enter the URL for the WSDL and then click Retrieve WSDL.

    • Click Specify the Local filename for the retrieved WSDL and browse to a file on your system.

    In either case, you must enter a package name (if it is not supplied), then click Finish. The new package appears in the project and includes an interface file and a stub file.

  4. You can now edit your source files to call the content the stub provides, then build and run.

    See "Creating a New Mobile Web Service Client" for a step by step process, or see "Run JSR172Demo" and view the demo source files.

19.2 Creating a New Mobile Web Service Client

This sample procedure creates a new project and adds a web service client. However, you can add a web service client to any MIDP project, it does not have to be new.

If you are using a proxy server, you must configure the emulator's proxy server settings as described in "Configuring the Web Browser and Proxy Settings."

  1. Click the File menu and select New Project, choose the Java ME category, and the Mobile Application project type, and click Next.

    Figure 19-1 Creating a New Project and Adding a Mobile Application

    Description of Figure 19-1 follows
    Description of "Figure 19-1 Creating a New Project and Adding a Mobile Application"

  2. Name your project and check Create Default Package and Main Executable Class. Click Next. Be sure to create Hello MIDlet for this example.

    Figure 19-2 Creating the Main Executable Class

    Description of Figure 19-2 follows
    Description of "Figure 19-2 Creating the Main Executable Class"

  3. Ensure that Java ME SDK is the platform and choose a device. Click Next.

  4. Right-click on the Source Packages node and select New > Java ME Web Service Client....

  5. In the New Java ME Webservice Client page:

    Click Finish.


    Note:

    Many additional project options can be found under the New --> Other menu item.


    Figure 19-3 The Java ME Web Servce Client Information Dialog Box

    Description of Figure 19-3 follows
    Description of "Figure 19-3 The Java ME Web Servce Client Information Dialog Box"

    The new package appears in the Source Packages tree and includes Shakespeare.java and Shakespeare_Stub.java.

  6. Edit HelloMIDlet.java as follows:

    • At the beginning, replace the default import declarations with:

      import javax.microedition.lcdui.*;
      import javax.microedition.midlet.*;
      import wsshakespeare.*; 
      
    • Locate the startApp() method and replace its contents with the following code:

      String text;Shakespeare s = new Shakespeare_Stub(); try {     text = s.getSpeech("Romeo"); } catch (java.rmi.RemoteException rex) {     text = "error";     System.out.println(rex.getMessage()); } TextBox t = new TextBox("Hello", text, 2048, 0); final Command exitCommand = new Command("Exit", Command.EXIT, 1); t.addCommand(exitCommand); t.setCommandListener(new CommandListener() {     public void commandAction(Command c, Displayable d) {       if (c == exitCommand) {    notifyDestroyed();       }     } }); Display.getDisplay(this).setCurrent(t);
      
  7. Build and run the project. You see a quote from Shakespeare's Romeo and Juliet on the device screen.

    You can vary the above procedure to use a local WSDL file. Open the following web page in a browser:

    http://www.xmlme.com/WSShakespeare.asmx?WSDL

    Save it to a local file. For example, C:\ws\WSShakespeare.wsdl. Follow the procedure above, except at Step 4, specify the local file name.

19.3 Run JSR172Demo

JSR172Demo shows how to access a web service from a MIDlet. The web service in this demo is running on an Internet server, and it conforms to the J2ME Web Services Specification. The client is the MIDlet running in the emulator

If you are using a proxy server, you must configure the emulator's proxy server settings as described in "Configuring the Web Browser and Proxy Settings."

Set Up the GlassFish Server

This demo requires the Oracle GlassFish server.

Set Up Environment Variables

Set the following environment variables:

JAVA_HOME=Java-installation-path

GLASSFISH_HOME=GlassFish-installation-path

Run the Demo Scripts

The scripts in the \server subdirectory assume that your server has a domain named "domain1" in which the service is automatically deployed. If you do not have a domain named domain1, set up this domain or edit run.bat and specify a domain you already have. Run the scripts:

demo_directory\JSR172Demo\server\build.bat

demo_directory\JSR172Demo\server\run.bat

Verify the Web Service

You see a page titled Web Services that displays information about the service including links to the WSDL file corresponding to the localhost url.

Run the MIDlet in the Emulator

JSR172Demo contains a single MIDlet named Server Script. Launch it and follow the prompts. You can browse through fictitious news headlines, all of which are retrieved from the web service.