Web Services with JSR 172
Web Services are software components that allow machine-to-machine interoperability and are platform and programming language independent. A WSDL (Web Service Description Language) file describes a web service encoded in XML format. The JSR 172 specification is an optional package that helps J2ME programmers to consume services inside a J2ME application and is designed to work with J2ME profiles based on either the Connected Device Configuration (CDC) or the Connected Limited Device Configuration (CLDC 1.o or CLDC 1.1).
To work with JSR 172, we need to generate “stub classes”, which will make all the “hard work” inside a J2ME application, such as the connection with the remote host. To do this, we will need to use the “Stub Generator” tool, available in the WTK (Wireless Toolkit) from Sun Microsystems. Once you have a WSDL file of your service, everything will be straightforward as we can see below.
Firstly, we set the WSDL filename or its URL in the upper field. Secondly, we set the stub classes output path, which can be the project’s “src” folder. Finally, we set the package output where the stub source code will be saved (in this case, br.Services). Press “Ok” to generate the stub classes.
In our example, we have a method called “echo” from MyService service, which receives a string and returns it. MyService_PortType_Stub class was generated in the mentioned process and it will open the connection with the remote host and make “echo” method available. A MyService_PortType_Stub instance will access “echo” method in a “localhost view”, however, in a background view, this method will serialize and send data to the remote host, as we can see below:
String echoStr = null; MyService_PortType_Stub myStubObject = new MyService_PortType_Stub(); try { echoStr = myStubObject.echo("J2ME Programming"); } catch (RemoteException e) { e.printStackTrace(); } this.formObj.append(new StringItem("Echo", echoStr));
The result is:
Albeit an easy solution, JSR 172 are not available for all devices. So, before using this JSR, be carefull the range of devices you are focusing to.
[]’s


Leave a comment