Tuesday, 21 June 2011

J2ME-SOAP

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;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;



public class J2meSoap extends MIDlet implements CommandListener{

    private Form form;
    private TextField barcode;
    private Command ok,cancel;
   
    private static final String NAMESPACE = "com.db.mobi.DBWEB";
    private static final String URL =
        "Connection - url";   
    private static final String SOAP_ACTION = "DBWEB";
    private static final String METHOD_NAME = "getBarcodeDetails";
    private HttpTransport    transport;
    private SoapObject        request;
    private ClassMap            classMap;
   
    public J2meSoap() {
       
        form=new Form("J2ME-SOAP");
        barcode=new TextField("Barcode","123456789012" ,100, TextField.ANY);
        ok=new Command("OK",Command.OK, 1);
        cancel=new Command("Exit",Command.EXIT,2);
       
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    notifyDestroyed();   

    }

    protected void pauseApp() {
       

    }

    protected void startApp() throws MIDletStateChangeException {
       
Display disp=Display.getDisplay(this);
form.append(barcode);
form.addCommand(ok);
form.addCommand(cancel);
form.setCommandListener(this);
disp.setCurrent(form);
       
    }
public void commandAction(Command cmd,Displayable dis)
{
    String label=cmd.getLabel();
    if(label.equals("OK"))
    {
        getbarcode(barcode.getString().toString());
       
    }else
    {
        try {
            destroyApp(true);
        } catch (MIDletStateChangeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
public void getbarcode(String barnum)
{
    System.out.println("Barcode==="+barnum);
   
    SoapObject objResult = (SoapObject) callService(METHOD_NAME);

    System.out.println("Result=="+objResult);
    if ( objResult != null )
    {
        String result = objResult.toString();
        System.out.println("Result=-==="+result);
    }//end if ( objResult != null )
}
private Object callService( String methodName )
{   
    Object    result = null;

    try
    {
        transport = new HttpTransport( URL, SOAP_ACTION + "#" + methodName );
        transport.debug = true;
   
    classMap = new ClassMap();
    classMap.prefixMap = new PrefixMap( classMap.prefixMap, "BarNum", NAMESPACE );
    transport.setClassMap( classMap );
  
    request = new SoapObject(NAMESPACE, methodName );
    request.addProperty("barcode", barcode.getString().toString());
   
    result = transport.call( request );
    }
    catch( Exception e )
    {
    e.printStackTrace();
    System.out.println( "Exception Request: \n" + transport.requestDump );
    System.out.println( "Exception Response: \n" + transport.responseDump );
    result = null;
    }//end try/catch
   
    return result;
}//end callService()

}