Experiencing with kSoap 2 and complex objects

As usual, I stumbled upon a problem, but this time during application development for Android.(Jump right into the problem explanation)

Imagine having to develop the application so that it can communicate with a webservice that you have already built some sort of programming language, such as in Java by using Axis2 (maybe in version 1.5.1) and a tomcat server (maybe in version 6).

kSoap 2 is an API designed to enable communication between mobile applications programmed in Java and web services.

I’m not very experienced in the use of this API, but it seemed a common problem of many people who use it, the impossibility of working with a list of objects, or rather with arrays of objects coming back as a result of a method of ur webservice.

Since my problem was a ClassCastException, due to the impossibility of casting the type ‘anyType‘ into something useful, like an object of my creation, or maybe an array of my objects, the solution was found using a method similar to Russian dolls.

Now let me explain better.

In the side of the web service application, you use data types that you created through the classes, but maybe you should call them by their name objects.

These objects are Java Beans suitably designed to be serialized into SOAP messages through the use of Axis2.

Axis2 supports the return lists of objects, so that through the use of SoapUI you can read the SOAP message response from the webservice and note the list of returned items. Even more, in the WSDL file that describes the web service, the return value of this method is marked as ‘sequence‘ object of our type.

The problem is kSoap that fails to convert and manage complex data type as the array of our objects, declaring it as ‘anyType‘.

Fortunately it is possible to decompose the object ‘anyType‘ for extracting fields(actually is a SoapObject, and we can use his methods like .getProperty(index) and .getPropertyCount() ).

The problem is that it does not handle arrays, it only returns the first object instead of the whole list. At this point I had to create an additional class that enclose my array of objects and then I edited the method of the webservice to return this new ‘wrapper‘ object.

kSoap doing so receives a single object that contains within it a sequence of objects that make up the array correctly serialized by Axis2. Here is an explanation of the Russian dolls.A big complex and custom object that include small complex objects.

Is our task in the client side of android, provide to the unwrapping of the object, that is remapped into the corresponding complex-object instances.
This is not a solution but a patch to the problem, a patch that I decided to take as we have upcoming deadlines on the development/deployment of the application.

Here is a picture to express a thousand words I wrote and made you read so far.

Drawing done with Balsamiq Mockups (for Desktop [demo]).

Here is a sample of code that unwraps soapObjects using .getProperty(index) and .getPropertyCount().

http://pastebin.com/JMpeYFK5

 

 

Recap:

webservice-side:

first I create my array of AccessoBean objects, then I give it to  the

.setListaAccesso(AccessoBean[] list)

method of my ArrayOfAccessoBean wrapper object.

My object  ‘AccessoBean’

http://pastebin.com/85dhXXsf

My wrapper object for an array of  ‘AccessoBean’ (how to do a simple wrapper class)

http://pastebin.com/eTBzap3x

So, instead of returning an array( ‘list’ of objects ), I’m returning a single object that contains my array of objects.

Please let me know if you solved this problem in another and/or more elegant way!

16 thoughts on “Experiencing with kSoap 2 and complex objects

  1. What about…
    ———————-
    private static Transacao readEnvelope(SoapObject envelopeSOb) {
    return new Transaction(
    envelopeSOb.getPropertySafelyAsString(“ClientNumber”),
    envelopeSOb.getPropertySafelyAsString(“Server”),
    envelopeSOb.getPropertySafelyAsString(“Transaction”),
    envelopeSOb.getPropertySafelyAsString(“DateServer”),
    }
    ————————
    ?

    • Nice one! thanks! so instead of using .getProperty(index) and .getPropertyCount() methods, I can now only use getPropertySafelyAsString() !
      Time ago I’ve read about java reflection , and registering custom type to java-system-somewhat .. It could save us the need to wrap array of objects and pass the whole array, well, without wrapping.. When I have enough time, I’ll try to give a try to both things! thank a lot man! appreciated!🙂

  2. You’re welcome.
    I had problems with the diferent returns of my WS, by now, it’s ok, (maybe some cpu cycles lost comparing strings).

    • Thank man! This is another good, well done example to show how to use ksoap with arrays, but with wrapping/unwrapping method : you say “I manually moved the properties into Query objects using the following code…” and you show how to pull data from soapobjects like I did, but do you know if there’s a way to automagically cast the soap object vector into a vector of our own objects? The problem of this cast, when I tried, where the famouse anyType error. I was reading about System.setProperty() method to register my own class in both server side and client side, but I haven’t tried yet..

      • Hi! a bit late…but… i’m exactly and this point:

        “do you know if there’s a way to automagically cast the soap object vector into a vector of our own objects?”

        Right now i iterate the properties like you do (piu o meno). But did you find the answer to the question?

        Grazie mille🙂

        David

      • Hello there! Ciao!!
        I haven’t got the time yet to work on it, but I give you a clue of what I’m gonna try to solve this problem, letting us, hopefully, to cast directly the object into the right class: it is called Java reflection api.
        I have not idea of what I’m saying but as soon as I recreate the infrastructure with a server holding tomcat and axis, I’ll try to solve this problem, maybe with a simple example but with the same initial problem! Stay tuned!

  3. I tried what they said to get the auto casting to work. I could not get it to do what was said or perform as shown in the code examples on the sites of other people. I suspect they wrote their code based on the theory and not working examples.

    Piergui, I have updated my post and provided an example of working with RowSet objects built from WebRowSet XML documents. The code can be found here:

    http://roderickbarnes.com/blog/droid-chronicles-web-services-handling-complex-parameters

    Let me know if you get the code to cast to a Vector of your particular object type. By the way, this last example includes charting.

    In His grip by His grace,
    Roderick L. Barnes, Sr.

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...