Browse DevX
Sign up for e-mail newsletters from DevX


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

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

Calling the Web Service with J2ME JAX-RPC
In the commandAction method of the GetQuoteMIDlet, when the user has hit the GetQuote command, the first of the two methods is invoked. Namely, the MIDlet calls the requestQuote method.

if (label.equals("Get Quote")) { requestQuotes(); }

The requestQuote method collects all the client data out of the EntryForm and then requests to start a separate GetQuoteMIDlet thread.

new Thread(this).start();

You might have noticed that the GetQuoteMIDlet implements the Runnable interface. Why does requestQuote start a new thread? If the operation was done in the commandAction method or other MIDlet method, then the MIDlet would have to wait for the Web service to respond. This could end up blocking the main Event Loop and other operations. Putting the call to the Web service in its own thread allows the MIDlet to continue to respond to the user (to answer the phone for example). In fact, if you place the call to the Web service in the same thread, you get the following warning when you run your application:

Warning: To avoid potential deadlock, operations that may block, such as networking, should be performed in a different thread than the commandAction() handler.

As the GetQuoteMIDlet implements the Runnable interface, it is in the run method creates an instance of the Web service stub class you generated earlier (Quote_Stub), sets up some properties on that instance, and makes a call to the Web service.

try { service = new Quote_Stub(); service._setProperty(Quote_Stub.SESSION_MAINTAIN_PROPERTY, new Boolean(true)); String xmlStr = service.getQuote(name, age, married, children, salary, smoker); parseQuotes(xmlStr); } catch (Exception exception) { Alert serviceProblem = new Alert("Service problem","Try your request later",null, AlertType.ERROR); serviceProblem.setTimeout(Alert.FOREVER); display.setCurrent(serviceProblem, form); }

Because of the potential for a remote exception (remember the RMI RemoteException that was part of the Web service interface?), the call is wrapped in a try catch block. A small part of the java.rmi package has been bundled with JAX-RPC just for this purpose. Where else do you see JAX-RPC here? If you look at the generated code, the Quote_Stub class implements the Stub interface from JAX-RPC and the other classes and methods are also from the J2ME JAX-RPC package.

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