Friday, October 14, 2011

asmx (ksoap) webservice example in j2me



import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
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.*;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;


public class asmx extends MIDlet implements CommandListener{
    Form f;
    Display display;
    Command com;

    TextField txx;

    public void startApp()
    {
        f=new Form("asmx");
        txx=new TextField("Celsius:", "", 100, TextField.NUMERIC);
         f.append(txx);
        com =new Command("ok", Command.OK, 0);
        f.addCommand(com);
        display=Display.getDisplay(this);
        f.setCommandListener(this);
        display.setCurrent(f);

    }

    public void pauseApp()
    {

    }

    public void destroyApp(boolean unconditional)
    {

    }

    public void commandAction(Command c, Displayable d) {

        if(c==com)
        {
           // services2();
            service();
        }

        throw new UnsupportedOperationException("Not supported yet.");
    }
   
    public void service()
    {
        Thread ss=new Thread()
        {
           public void run()

        {
        System.out.println("Entrando a Services2");
    String serviceUrl = "http://www.w3schools.com/webservices/tempconvert.asmx";
    String serviceNameSpace = "http://tempuri.org/";
    String soapAction = "http://tempuri.org/CelsiusToFahrenheit";
    String methodName = "CelsiusToFahrenheit";
    SoapObject rpc = new SoapObject(serviceNameSpace, methodName);
    rpc.addProperty ("Celsius", txx.getString().trim());
   
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.bodyOut = rpc;
    envelope.dotNet = true;//<strong>IF you are accessing .net based web service this should be true</strong>
    envelope.encodingStyle = SoapSerializationEnvelope.XSD;
    HttpTransport ht = new HttpTransport(serviceUrl);
    ht.debug = true;
    ht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    String result = null;
   
    try
    {
   
    ht.call(soapAction, envelope);
       
    Alert alert = new Alert("Result",
                        ht.responseDump,
                        null, AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert, f);
     
    }
    catch(org.xmlpull.v1.XmlPullParserException ex2)
    {
    Alert alert = new Alert("Error",
                        ex2.getMessage(),
                        null, AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert, f);
    }
    catch(Exception ex)
    {
    Alert alert = new Alert("Error",
                        ex.getMessage(),
                        null, AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert, f);
    }
        }
        };
        ss.start();
    }
}

No comments:

Post a Comment