When you are thinking about using web services with Java on mobile devices there are 2 main parsers you can use. These are:
- J2me web services (JSR172) (for more details you can read this introductory article: http://developers.sun.com/mobility/apis/articles/wsa/)
- kSOAP 2.0 (for more details look at the project homepage here: http://ksoap2.sourceforge.net/).
A good explanation of the differences are explained here http://www.smalllinks.com/8D7. To summarise they are:
- kSOAP supports DOM, XMLpull and SAX whereas JSR172 supports SAX only
- kSOAP is document centric whereas JSR172 is Java centric
- JSR172 supports SPI (see JSR172 URL above for more details)
What I will do in this tutorial is show the difference in the code for using these two technologies for creating mobile web service clients. For this tutorial I would not recommend using Oracle BPEL process manager to create your web services unless you have an idea of how to change the SOAP messaging format. For this tutorial I am using the Java Wireless Toolkit 2.5.2 for the mobile development and assume that you have some experience with creating MIDLets.
The first thing you will need to do is to create a simple HelloWorld web service in your chosen web service platform. This should have a method that takes a string field so that a user can enter their name as an input. The method should then append the “Hello ” and the persons name as an output.
1. Using JSR172
Setting up: The first thing you need to do is make sure your project is set up for using web services, I am going to use the Nokia NE51 as my test phone so I will use the MIDP2.0 and CLDC1.1 profiles so make sure these are checked under API selection in the settings tab. You must also make sure that JAXP XML Parser and Web Services API are checked.
Creating the stubs: Once you have done this you will need to create Stubs from the WSDL of the web service you have created. You must go the the ‘Stub generator’ which is under the ‘project’ menu. You will then need to navigate to your WSDL and give the output folder a name. Now if you look in your application folder under the ‘src’ folder you will find a new folder containing a package that contains all of the stubs. If you get the error: java.net.MalformedURLException when creating your stubs dont worry, this is because the wsdl document file path has a space in. Simply move it to your c: drive and it will create the stubs without a problem.
Creating the MIDlet: Now you need to create a simple client application MIDlet and you will need to import the the package with the stubs in. The next thing you must do is create a varible of the [service name]_portType_Stub. Once you have done this you can start calling the classes. Here is a code snippet that shows how to use the stubs:
try {
// create the stub
service = new HelloWorld_Stub();
service._setProperty(HelloWorld_Stub.SESSION_MAINTAIN_PROPERTY, new Boolean(true));
// call the method
returnString = service.sayHello(inputName.getString());
// display the response
outputForm.append(“JSR172 response: ” + returnString);
display.setCurrent(outputForm);
} catch (Exception exception) { …. }
Note: when calling the stubs there is a possibility that you may get an invalid element in response error. If this happens use the Network monitor to see if the SOAP message is coming back correctly. If this is the case there could be a namespace problem with a complex type if this is the case go to the stub and change the namespace URI to match what is returned in the message.
Setting up: When using kSOAP the first thing that you must do is to download kSOAP2 and then copy the core libary into the lib folder in the project. You will then need to import them into your MIDlet like this:
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.serialization.SoapObject;
- import org.ksoap2.serialization.
- SoapSerializationEnvelope;
- import org.ksoap2.transport.HttpTransport;
Creating the MIDlet: Once you have done this you are then ready to start using kSOAP. It is actually quite simple. I have anotated the code I have used to call the web service:
try {
// set up the variables
String endPointURL = new String(“http://[host]/HelloWorld/services/HelloWorld”);
String nameSpace = new String(“http://hello”);
String soapAction = new String(“http://hello”);
String urn = new String(“urn:HelloService”);
// create the envelope
SoapObject soap = new SoapObject(urn, “sayHello”);
soap.addProperty(“name”, inputName.getString());
// serialize the envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = soap;
// set up the transport
HttpTransport ht = new HttpTransport(endPointURL);
//make the call
ht.call(“”, envelope);
//get the response
Object soapIn = envelope.getResponse();
// Do something with the response
outputForm.append(“kSOAP response: ” + soapIn.toString());
display.setCurrent(outputForm);
} catch (Exception exception) {…}
When you create the SOAPObject the second argument is the method in the service that you wish to call. In this example its the sayHello method.
3. Device screenshots
The screenshots below show the MIDlet running on the device. The first screen lets you enter a name and then choose the parser type. The second screen is the JSR172 response and the third screen is the kSOAP response.
4. Final word
As you can see using both of these technologies is quite simple once you get over the main hurdle of knowing how to set them up. The mobile space is really interesting at the moment and I’m sure that web services will have a large role to play in the delivery of apps and content to mobile devices in the future.




Hi! I’m developing a j2me web service client and finding the solution for an error, I read a post of your blog. The problem is that I have a Invalid element in the response error, and I thinks that is because my web service return a complexType which name is Pelicula_Mini, I think that the Stub generated by Sun Java WTK does not have included it, I think that I have to included the complextype in somewhere or change the uri os the attributes of the complextype but I don’t know how to do it.
Thanks a lot, and sorry for the large text
Mª Jesús
I have had this problem too, but it was a long time ago when I did this. Have you looked through the classes generated by the wtk to make sure they correspond to the complex type. If I remember correctly I had to change the generated class slightly to match what was being sent by the web service.
In the class which name end with _Stub, it failed in this operation:
resultObj = op.invoke(inputObject);
and the definition of the namespaces are these:
protected static final QName _qname_buscarPelicula = new QName(“http://DefaultNamespace”, “buscarPelicula”);
protected static final QName _qname_buscarPeliculaResponse = new QName(“http://DefaultNamespace”, “buscarPeliculaResponse”);
protected static final QName _qname_buscarPeliculaReturn = new QName(“http://DefaultNamespace”, “buscarPeliculaReturn”);
protected static final QName _qname_claseMini = new QName(“http://DefaultNamespace”, “claseMini”);
protected static final QName _qname_claseMiniResponse = new QName(“http://DefaultNamespace”, “claseMiniResponse”);
protected static final QName _qname_claseMiniReturn = new QName(“http://DefaultNamespace”, “claseMiniReturn”);
protected static final QName _qname_pelicula_Mini = new QName(“http://clases”, “Pelicula_Mini”);
protected static final QName _qname_dispositivo = new QName(“http://DefaultNamespace”, “dispositivo”);
protected static final QName _qname_t = new QName(“http://DefaultNamespace”, “t”);
protected static final QName _qname_anno = new QName(“http://clases”, “anno”);
protected static final QName _qname_director = new QName(“http://clases”, “director”);
protected static final QName _qname_genero = new QName(“http://clases”, “genero”);
protected static final QName _qname_id = new QName(“http://clases”, “id”);
protected static final QName _qname_sinopsis = new QName(“http://clases”, “sinopsis”);
protected static final QName _qname_titulo = new QName(“http://clases”, “titulo”);
protected static final Element _type_claseMini;
protected static final Element _type_claseMiniResponse;
protected static final Element _type_buscarPelicula;
protected static final Element _type_buscarPeliculaResponse;
and the wsdl type is this:
and the error is:
java.rmi.MarshalException: Invalid Element in Response: anno
at ClienteMovil.ConsultarPelicula_Stub.buscarPelicula(+75)
at principal.consultarPelicula.run(+28)
I hope you can help me, this proyect is so important for me (It is for finish my degree in computer science) and I have a month with no solution for this mistake =(. Thanks a lot one more time.
Have you checked the network monitor to make sure you are getting the correct response from the web service?
When I use the network monitor I have no response (the response is empty) =(
OK. So, does the request look OK?