The current version of kSOAP2, the Android library for communication via SOAP, does not offer facilities for serializing and deserializing complex objects. Furthermore, arrays are not supported.
In this post, we will introduce a patch of kSOAP2 which enables Android clients to communicate complex objects and arrays via SOAP. We will do that with the help of an example.
Here you can find:
To show how to use the patched library, let us consider a simple Soap Server, created thorugh Axis2 1.5, which stores information about students and exams. A student has some personal information (name, address, telephone number…), a lists of exams, and a tutor professor. As we can see from the server wsdl, the server publishes a method “put” with parameter “studente” of type “Studente”, and returns a result of type “int”, which is the newly created student identifier.
To communicate with the server, first of all we have to create java classes for every complex object sent or received throught the Soap message.
So let’s start creating the object “Studente”, needed by the method
“put” of the server. You can find the complete source of class
Each complex object which has to be serialized or deserialized must be in a class which extends
For instance, in order to create the class for Studente, we must inspect the wsdl of the server and insert every required field into Studente class:
As a subclass of DataSoapObject, Studente must redefine
initMapping, in order to specify the mapping between itself and the
name which will be found on the wsdl (which in our case is
We must remember to call
We must provide a constructor for Studente with arguments namespace and wsdl name, like this:
and that’s all. We can enrich Studente class with getter and setter for every field, with others constructor if needed, with a
WARNING: to be correctly deserialized, every complex object must contain at most one plain array of objects.
Performing a call to the server is done in the standard way; i didn’t
need to change anything in its use, but be sure to be using my
As usual, we prepare the transport with URL, namespace and name method we want to call:
We create a student to be sent to the server and we add it to the request, using the name of the parameter of the method put, specified in wsdl file:
We create an envelope and we insert the request into it:
Then, we perform the call to the server:
and finally we wait for the response, which in this case is the identifier of the newly inserted Studente:
You can find the source of MainActivity, here.
Note that in the sources you can also find an object named Prova, used to test purpose: it contains arrays and all sort of types (int, Integer, long, Long, etc..)