For information about the design and functionality of the MIDlet, see section Design.
For information about the key classes and interfaces needed to implement the MIDlet, see:
The CapitalMIDlet class is the core class in the MIDlet. It controls the UI and communicates with the Poster class that performs the actual web service call. The CapitalMIDlet class also implements the PosterListener interface so that it can get the result of the asynchronous remote calls.
The CapitalPortType is a simple interface generated from the WSDL stub. Do not modify it manually.
The interface contains the definition of the remote operations as defined in the WSDL file. Notice that the interface extends java.rmi.Remote and, according to the Java RMI conventions, all operations include RemoteException in their signature.
import java.rmi.Remote; import java.rmi.RemoteException; public interface CapitalPortType extends java.rmi.Remote { public String getCapital(String aNation) throws java.rmi.RemoteException; }
The CapitalBinding_Stub class is generated from the WSDL stub. It contains the actual functionality needed to connect to the JAX-RPC runtime.
The CapitalBinding_Stub class implements the stub so that the _get and _setProperty methods can be used to set the characteristics of the connection to the web service. In particular, the ENDPOINT_ADDRESS_ PROPERTY value is set to match the address of the service.
The Poster class handles the actual remote procedure call. It does this in a separate thread and delivers the result or error message asynchronously. The separate thread is crucial, since making the remote procedure call from the UI thread can cause conflicts.
To implement the Poster class:
Create the Poster.java class file.
Import the required classes.
import javax.xml.rpc.Stub; import java.rmi.RemoteException;
Set Poster to implement Runnable . MIDlets use the Runnable interface to execute threads.
public class Poster implements Runnable {
Create the required variables and the Poster class constructor. In the constructor, set up a PosterListener interface that allows the MIDlet to receive the results of asynchronous remote calls.
// true if IO thread is running, false if not private boolean isThreadRunning = false; // The listener private PosterListener listener; private String nation; private String endPoint; public Poster(PosterListener listener, String endPoint) { if (listener == null) { throw new IllegalArgumentException("Listener cannot be null"); } this.endPoint = endPoint; this.listener = listener; }
In the requestCapital method, start a new thread to handle the remote procedure call.
public synchronized void requestCapital(String country) { if (!isThreadRunning) { isThreadRunning = true; this.nation = country; new Thread(this).start(); } }
Handle the IO operation in the new thread.
public void run() { CapitalBinding_Stub capitalServiceStub = new CapitalBinding_Stub(); capitalServiceStub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, endPoint); CapitalPortType capitalService = (CapitalPortType) capitalServiceStub; try { String capital = capitalService.getCapital(nation); listener.onCapitalRequestComplete(nation, capital); } catch (RemoteException e) { listener.onCapitalRequestError(e.getMessage()); } } }
The PosterListener interface is used to deliver the results of the remote procedure call or any errors produced during the call. The CapitalMIDlet class implements this interface and registers itself as the listener of Poster.
public interface PosterListener { /** * Called as a result of a succesful call containing the nation * and the resulting capital */ public void onCapitalRequestComplete(String nation, String capital); /** * Called as a result of an error while calling the WebService */ public void onCapitalRequestError(String code); }