In this article we will see how to connect to a .NET Web Service from a j2ME MIDlet using kSOAP2 library. The Java 2 Micro Edition (J2ME) Platform offers support for Web Services through the J2ME Web Services API (WSA), JSR 172, which provides two optional APIs: remote service invocation (JAX-RPC) and XML parsing (JAXP). For the next example, we will use the third party kSOAP 2 library because it is lightweight and efficient. The solution is developed in the NetBeans IDE but you could use also Eclipse IDE.

1. Download the latest version of the kSOAP 2 library which is available on the project sourceforge page (http://sourceforge.net/projects/ksoap2/). At this moment, the archive of the kSOAP 2 library is ksoap2-j2me-core-2.1.2.jar

In the MIDlet startApp() method, that is called when the application is starting, we initialize the Display reference and display the previous form.

publicvoid startApp(){//get the Display referenceif(display ==null)
display = Display.getDisplay(this);//display the main form
display.setCurrent(mainForm);}

6. Because we want to use the Exit and Add commands we define the handler for the Command event by implementing the CommandListener interface and overriding the commandAction(Command c, Displayable d) abstract method:

publicvoid commandAction(Command c, Displayable d){}}

7. Register the form as the command listener in the MidletkSOAP startApp() method:

//register the command listener
mainForm.setCommandListener(this);

Important !

Do NOT forget to register the listener. By implementing the CommandListener interface you only define a possible listener.

How to use kSOAP 2 to connect to a .NET Web Service from a J2ME MIDLet

The architecture of the solution is based on next activities:

The arhitecture of thr kSOAP MIDlet

the user insert the two values and select the Add command

the kSOAP library envelopes the values in a SOAP request and sends the message to the .NET Web Service; sending the request is the event that calls the remote method;

the application gets the SOAP response that contained the result returned by the remote method;

using the kSOAP library, the result is extracted from the response and it is displayed.

9. We define a new method, callWebServiceMethod(), that will encapsulates the kSOAP logic. The core of the kSOAP 2 library is represented by the SoapObject class which is used to manage SOAP requests and responses. The SOAP object is described by:

the Web Service namespace

the method name

In the callWebServiceMethod() we define two String constants, to store the SOAP object attributes values. With these two values we create the SOAP object:

10. As we have seen in the .NET Web Service source code, it has the add(int a, int b) method, that receives two integer values as arguments. The method parameters are also encapsulated in the SOAP request using the addProperty() method:

11. In order to generate the XML based request we must use a SoapSerializationEnvelope (from org.ksoap2.serialization package) object. When we construct the SoapSerializationEnvelope we must decide which is the SOAP protocol version we want to use. For the .NET Web Service, from this example, we will use SOAP 1.1.

Because we are connecting to a .NET Web Service we set the .dotNet property of the SoapSerializationEnvelope object to true.

Also, the previous SoapObject object is set as the outbound message for the SOAP call.

12. The XML SOAP request is sent over HTTP using a HttpTransport (org.ksoap2.transport package)object. For the remote call we need the Web Service URL and the SOAP action (the Web Service namespace plus the method name).

If you have problems with the examples or you don’t understand our explanations, put a question in the comments and we will help you. Also, any suggestion or comment that will improve this material is appreciated.

Like it? Then share this post or check the external adds. Sharing is the best way to appreciate the author.