Thursday, June 9, 2011

How to access web service using J2ME code


For calling web services j2me required stub files for it. Each and every web service class is called using its stub.

This document will show sample web service and will teach you how to create stub files

Sample web service URL

http://www.webservicex.net/stockquote.asmx


Generating the Stub Class

We need the Web Service Description Language (WSDL) description for the service. Luckily, .NET services supply this for you is you append "?WSDL" to the URL.
The Java ME SDK contains a tool called "wscompile", that reads the WSDL data and generates a "stub" Java class. This class acts as a local proxy for the remote service. You call a method in the stub class, and it calls the remote method for you.
To generate the stub class, we need a config.xml file.
<?xml version="1.0"?>
<configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
 <wsdl location="http://www.webservicex.net/stockquote.asmx?WSDL" packageName="rpcdemo" />
</configuration>
The wsdl location must match the URL for the service (with "?WSDL" tacked on the end). The packageName is the package for the generated files.
\Java_ME_platform_SDK_3.0\bin\wscompile.exe -gen -cldc1.1 config.xml
You should specify -cldc1.1 if the web service might use floats or doubles as arguments or return value.
After executing this, you should have a file (amongst others) for the class: rpcdemo.StockQuoteSoap_Stub 





Midlet for calling Web service


import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;


public class RpcDemo extends MIDlet implements CommandListener, Runnable {


    private Form form;


    public void startApp() {
        if (form == null) {
            form = new Form("RpcDemo");
            form.addCommand(new Command("Exit", Command.EXIT, 0));
            form.setCommandListener(this);

            // get the data
            (new Thread(this)).start();
        }
        Display.getDisplay(this).setCurrent(form);
    }


    public void pauseApp() {
        // empty
    }


    public void destroyApp(boolean must) {
        // empty
    }

    public void commandAction(Command c, Displayable d) {
        if (c.getCommandType() == Command.EXIT) {
            notifyDestroyed();
        }
    }


    public void run() {
        try {
            // create the stub
            StockQuoteSoap_Stub service = new StockQuoteSoap_Stub();
            // set the URL for the service
            service._setProperty(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, "http://www.webservicex.net/stockquote.asmx");

            println("Connecting...");
            // invoke the remote method
            String xmlResponse = service.getQuote("NOK");
            println(xmlResponse);

            println("Done.");
        } catch (Exception e) {
            println(e.toString());
        }
    }


    private void println(String s) {
        form.append(s + "\n");
    }
}

 

No comments:

Post a Comment