Monday, April 6, 2009

Creating The Web Services Client (Part 3)

We have created the desktop and web applications as the clients of web service. There one more application that we are going to create. The mobile application. This mobile application is based on Java (J2ME).

The Code

You need a supporting library to make your job easier. In this tutorial, I will use the Wingfoot SOAP or WSOAP as the supporting library. You can download WSOAP from www.sourceforge.net. The other SOAP implementation for mobile application is KSOAP.

WSOAP came in zipped file. Inside the zip file there are two essential files such as kvmwsoap_1.06.jar and j2sewsoap_1.06.jar. The one that we are going to use is the first one. So, copy the kvmwsoap_1.06.jar file to the lib folder inside your J2ME project folder.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import com.wingfoot.soap.*;
import com.wingfoot.soap.encoding.*;
import com.wingfoot.soap.transport.*;

public class wsclient extends MIDlet implements CommandListener,Runnable{
    private Form form;
    private TextField txtName;
    private StringItem strResult;
    private Command cmdOk;
    private Command cmdExit;
    private Display display;
    
    public wsclient(){
        form=new Form("Web Service Client");
        txtName=new TextField("Name","",10,TextField.ANY);
        strResult=new StringItem("","");
        cmdOk=new Command("Ok",Command.OK,1);
        cmdExit=new Command("Exit",Command.EXIT,2);
        
        form.append(txtName);
        form.append(strResult);
        
        form.addCommand(cmdOk);
        form.addCommand(cmdExit);
        form.setCommandListener(this);
    }
    
    public void startApp(){
        display=Display.getDisplay(this);
        display.setCurrent(form); 
    }
    
    public void pauseApp(){
    }
    
    public void destroyApp(boolean unc){
    }
    
    public void commandAction(Command c, Displayable d){
        if(c==cmdOk){
            new Thread(this).start();
        }
        if(c==cmdExit){
            destroyApp(false);
            notifyDestroyed();
        }
    }
    
    public void run(){
        try{
            // THE LOCATION OF WEB SERVICES
            String url="http://localhost:8080/axis/services";
            
            Call c=new Call();
            
            // SET THE THE METHOD NAME, SERVICE NAME, AND THE INPUT PARAMETER
            c.setMethodName("greeting");
            c.setTargetObjectURI("helloService");
            c.addParameter("name",txtName.getString());
            
            HTTPTransport transport=new HTTPTransport(url,null);
            transport.getResponse(true);
            
            // CALL THE WEB SERVICE
            Envelope res=c.invoke(transport);
            
            strResult.setText("Response : " + String.valueOf(res.getParameter(0)));
        }
        catch(Exception ex){
            strResult.setText(ex.toString());
        }
    }
}

Save this file with name wsclient.java under the src folder of your project.


Build and Run It!

Build your application and run it. This is the screenshot of this application while running.


No comments: