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

Previous
Previous
 
Next
Next
 

19 JSR 172: Web Services Support

The 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 also:


Section 19.1, "Generating Stub Files from WSDL Descriptors"
Section 19.2, "Creating a New Mobile Web Service Client"
Section 19.3, "Run JSR172Demo"

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 6.9.1, the "Mobility End to End" plugin must be installed.

If you are using NetBeans 7 or higher the "SOAP Web Services" plugin must be installed.


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 on the Source Packages node and select New > Java ME Web Service Client...

  3. In the Generate J2ME Webservice Stub page, you can either:

    • Click Running Web Service and enter the URL for the 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, 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 Section 19.2, "Creating a New Mobile Web Service Client" for a step by step process, or see Section 19.3, "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.

  1. Select File > New Project, choose MIDP application, and click Next. Name your project and ensure Create Hello MIDlet is checked. Click Finish.

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

  3. In the Generate J2ME Webservice Stub page:

    Click Finish. The new package appears in Source Packages and includes Shakespeare.java and Shakespeare_Stub.java.

  4. Edit HelloMIDlet.java as follows:

    • At the beginning, add the following import declaration:

      import testws.*

    • 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);
      t.addCommand(exitCommand);
      t.setCommandListener(this);
      display.setCurrent(t);
      
  5. 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 is already running on an Internet server, and it conforms to the J2ME Web Services Specification.

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

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