Introduction
Java Micro Edition (Java ME) is a platform for running applications on smaller devices such as mobile phones, PDAs etc. These devices have restrictions in terms of Memory and Processing power. Java ME defines various configurations and profiles. Midlets are java applications that operate on Mobile Information Device Profile (MIDP) which is targeted on Mobile phones that has limited memory and processing power. If you are new to Java ME please refer to the articles Introduction to J2ME and J2ME User Interface in Java Beat.
also read:
Due to the limitations on the mobile phones, the midlets can’t be exposed as a web service. But the midlets can consume web services. Java ME provides a subset of JAX-RPC APIs to work with web services. This article will provide a step by step procedure to access web services from a midlet.
Invoking web services from a Midlet
To access web services, the Midlet has to make remote calls on the web services. But the Midlets won’t do this directly. A stub has to be created and calls are made via the stub instance. Using wireless toolkit it is made very simple to create stub code. Invocation on web service method is a synchrous process. The parameters for the methods are passes by value instead of pass by references. Similarly the values are returned by web services as pass by value. On the stubs midlets can set some properties like USERNAME_PROPERTY, PASSWORD_PROPERTY (User name and password for authentication), ENDPOINT_ADDRESS_PROPERTY(Address of the endpoint) and SESSION_MAINTAIN_PROPERTY(if true:session will be maintained between endpoint and midlet) using _setProperty method.
stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,serviceUrl); stub._setProperty(Stub.SESSION_MAINTAIN_PROPERTY, new Boolean(true));
The generated stub code and the supportive classes have to be bundled in the jar file along with the midlet. While accessing web services the best practice is to write the sub invocation code in a seperate thread. This will avoid your application hanging as your thread will be blocked during web service invocation.
Example
This example will help you in understanding how to access web service from a midlet. Netbeans IDE, Glassfish application server and Wireless tool kit 2.5.2 are used.
Create the web service and deploy it in the application server
Web service:
package mypack; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService() public class HelloService { @WebMethod(operationName = "getMessage") public String getMessage(@WebParam(name = "name") String name) { return "Hi "+name+"! Welcome to JME Programming"; } }
WSDL URL:
http://localhost:17754/HelloWebService/HelloServiceService?WSDL
Create a Midlet with a form with a text field and command buttons.
public class Midlet extends MIDlet implements CommandListener{ private Form myform; private TextField field; private Command exit,getMessage; private Display d; public void startApp() { d=Display.getDisplay(this); myform=new Form("Access Webservice"); field=new TextField("Name", "", 20, TextField.PLAIN); exit=new Command("Exit", Command.EXIT, 0); getMessage=new Command("GetMsg", Command.SCREEN, 1); myform.append(field); myform.addCommand(exit); myform.addCommand(getMessage); myform.setCommandListener(this); d.setCurrent(myform); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if(c==exit){ notifyDestroyed(); }else if(c==getMessage){ //Code to Access Webservice } } }
Use Wireless tool kit tool to create the stub
(b) Select File -> Utilities
(c) Select Stub Generator
d) Provide WSDL URL and the Output folder(src folder in Netbeans project) and the package details. Click on Ok
(e) The Stub and the other classes are generated in the src folder
Access Web service
For accessing web service,
- Create a new thread.
- In the new thread, Instantiate the stub
- Set properties on the stub if required
- Invoke the web service methods on the stub
//Extend the code written earlier import mypack.HelloService_Stub; public class Midlet extends MIDlet implements CommandListener{ private Form myform; private TextField field; private Command exit,getMessage; private Display d; public TextField getField() { return field; } public Form getMyform() { return myform; } .... public void commandAction(Command c, Displayable d) { if(c==exit){ notifyDestroyed(); }else if(c==getMessage){ Thread t=new Thread(new NewThread(this)); t.start(); } } } class NewThread implements Runnable{ Midlet m; NewThread(Midlet m){ this.m=m; } public void run() { Form frm=m.getMyform(); TextField txt=m.getField(); HelloService_Stub stub=new HelloService_Stub(); String msg=""; try { msg = stub.getMessage(txt.getString()); frm.append(msg); } catch (RemoteException ex) { frm.append(ex.getMessage()); } } }
Run the Midlet
Run the midlet. Provide the Name and select the command button GetMsg. The following output will be displayed.
Conclusion
Java ME Midlets can act only as the consumer for web services. It can’t be exposed as a web service. Wireless Tool Kit helps in creating stubs for web services. Midlets by invoking methods on the stub invokes methods on web services. Both simple types and complex types can be passed as parameters to the web services.