/* * Copyright (C) 2011 www.itcsolutions.eu * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1, or (at your * option) any later version. * * This file is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * */ /** * * @author Catalin - www.itcsolutions.eu * @version september 2011 * */ package eu.itcsolutions.j2me.ksoap; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransport; /** * @author Catalin */ public class MidletkSOAP extends MIDlet implements CommandListener { //the main form Form mainForm = null; //the text-boxes for the input TextField txtBoxA = null; TextField txtBoxB = null; //the result label StringItem result = null; //the Exit command Command cmdExit = null; Command cmdAdd = null; //the Display reference Display display = null; public MidletkSOAP() { //construct the main form mainForm = new Form("kSOAP Example"); //construct the controls txtBoxA = new TextField("Value 1:", null, 5, TextField.NUMERIC); txtBoxB = new TextField("Value 2:", null, 5, TextField.NUMERIC); result = new StringItem("Result:", null); //add controls to the form mainForm.append(txtBoxA); mainForm.append(txtBoxB); mainForm.append(result); //construct commands cmdExit = new Command("Exit", Command.EXIT, 1); cmdAdd = new Command("Add", Command.SCREEN, 1); //add commands mainForm.addCommand(cmdAdd); mainForm.addCommand(cmdExit); } public void startApp() { //get the Display reference if (display == null) { display = Display.getDisplay(this); } //display the main form display.setCurrent(mainForm); //register the command listener mainForm.setCommandListener(this); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if (c == cmdExit) { this.destroyApp(false); this.notifyDestroyed(); } else if (c == cmdAdd) { callWebServiceMethod(); } } //the kSOAP solution public void callWebServiceMethod() { String NAMESPACE = "http://www.itcsolutions.eu/"; String METHOD_NAME = "add"; String SOAP_ACTION = "http://www.itcsolutions.eu/add"; String URL = "http://localhost:50827/WebServiceExample.asmx"; //create a SOAP object SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //add method arguments request.addProperty("value1", Integer.valueOf(txtBoxA.getString())); request.addProperty("value2", Integer.valueOf(txtBoxB.getString())); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransport j2meHttpTransport = new HttpTransport(URL); try { //if you want to debug, uncomment next line //j2meHttpTransport.debug = true; j2meHttpTransport.call(SOAP_ACTION, envelope); SoapObject content = (SoapObject) envelope.bodyIn; String sum = content.getProperty(0).toString(); //display result result.setText(sum); } catch (Exception e) { e.printStackTrace(); } } }