Reviving ksoap2 for Android

If you are developing Android applications that need to interface with SOAP web services, you probably know that the Android SDK does not have any support for this technology. It is somewhat considered legacy, but it is still widely used in many enterprise applications. When you look around for a SOAP library that is suitable for mobile devices in the sense that it is not too heavy you will inevitable find ksoap, which has a history on J2ME and Blackberry. Unfortunately the project has been pretty much dead for a long time now.

Luckily it was ported and adapted to Android and has been hosted on google code for a while. It works well enough, but at least the last couple of month the project was very quiet (=dead). Since I am using the library I have created a patch a while ago that fixes a problem with reading attributes and the article on my site got lots of readers. That also sparked patches being sent to me via email as well as on github. Since those patches looked good and I was going to do the work to use them anyway I decided to take over the dormant project on google code after multiple attempts to contact the previous owner in the last months. Within a day of launching the take over request I was the project owner and also got the okay from the previous project owner (thanks Karl and Google).

The next day I had a a bunch of patches applied to my github clone, that now acts as upstream as well as done a major cleanup of the wiki and issue tracker. I have also created a mailing list and hope that many of you will join me there.

Please understand that the project is basically in maintenance mode, so unless I find a problem that affects me I won’t be doing any active development. I am however more than willing to

  • take patches from you
  • answer questions where I can on the mailing list and stackoverflow
  • update the wiki and issue tracker
  • take new contributors for development, documentation and other tasks on board
  • cut new releases

So with that said, and a thanks to the people who have already contributed, enjoy the new 2.5.1 release with a bunch of fixes (e.g. attribute support, soapfault support and a bunch of convenience methods). The project is to some extend its all yours now. Bring on the help!

PS: Anybody want to contribute some sort of logo image? E.g. a soap like looking Android with some xml tags on it or whatever?

16 comments » Write a comment

  1. Pingback: Attribute support for KSOAP2 on Android | simpligility

  2. Hi! Thanks for all the help that you have provided so far. I have one issue that is bordering me for some time now… The thing is that I can not access array of objects returned by web service. I don’t know if this is just me doing something wrong or there is a bug in kSoap2.

    Here is the web service response:

    4
    6
    10

    and here is the code in Android using kSoap2
    .
    .
    .
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope =
    new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);

    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);

    t.setText(envelope.getResponse().toString()); //here only first element is returned, in this case 4

    It is the same issue with complex types. Only the first one will be accessible. All its atributes are there, but other objects in response are ignored. If I use bodyIn I can see all the data. And one more thing: if I use getPropertyCount() I get ResourceNotFound Exception, String resource ID:0x02.

    I’m using 2.5.1 version of kSoap.

    I would appreciate your help.

    Best regrads,
    Nebe

  3. Can you please cross post this question to the mailing list. I will try to look at it tomorrow and answer there…

  4. Hi once again.

    Sorry for bordering you. I did manage to find a solution. There is a bug in getResponse() method of SoapSerializationEnvelope class. This is the source code of that method:

    public Object getResponse() throws SoapFault {
    if (bodyIn instanceof SoapFault) {
    throw (SoapFault) bodyIn;
    }
    KvmSerializable ks = (KvmSerializable) bodyIn;
    return ks.getPropertyCount() == 0 ? null : ks.getProperty(0);
    }

    As you can see this method returns only first property. The work around that worked for me was this:

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope =
    new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);

    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);

    KvmSerializable ks = (KvmSerializable)envelope.bodyIn;

    for(int i=0;i<ks.getPropertyCount();i++){
    ks.getProperty(i); //this can be cast to SoapObject if complex type is returned.
    }

    Thanks for the response.

    Best regards,
    Nebe

  5. How about providing a patch that fixes the problem in the project. I could pull it in and do a new release soon.

  6. I would be glad to contribute. Just tell me in which form do you want me to provide the patch?

  7. Ideally you would clone the repo on github, patch it there and create a pull request. Otherwise just a patch file based on the github repo would be fine too. Your choice.

  8. hi, how to send xml as request like for user authentication,

    userName> stringstring12453543</token

    my doubt is where to pass the above xml as parameter,

    i am adding it in request.addProperty("tagName",abovexmlstring)

  9. You have to build up a soapobject that implements that rather than just getting straight xml as string into it. Please ask more questions on the ksoap2-android mailing list.

  10. I can’t request a webservice with more than one attributes. If I add no or one property everything went fine but if I am send a request with two attibutes I always get the an error “Server was unable to process request. —> Sequence contains no elements”. Does anybody have an idea?

  11. Nebe’s solution for the getResponse() bug worked in my case as well.
    Since it’s a pretty frequently used scenario to respond with a sequence of data in a SOAP response, I was quite suprised that this bug in KSOAP2 only has so little talk-around on the web. Should be fixed ASAP in the next rev.

  12. The fix is already applied in the project codebase and will be part of the next release. For now feel free to build it yourself and use the snapshot version.Otherwise hold on for a bit.

  13. i am using older version of ksoap2 library
    how to implement request timout in kasoap2 library and from where to download latest ksoap2 library?

    thanks umar

  14. All the necessary links and documentation is on the project site linked in the article above.

  15. Hi,
    I am wondering how to work with a complex object that has as an attribute an array of strings. I have found a lot of questions regarding this on google search but no answer yet except maybe Marshalling.
    A simple example of what I am talking about would be:

    public class Message {
    String[] data;

    … getters/setters
    }
    I left a message the other day on the mailist.

    Thanks!
    -Tony
    PS: If you consider SOAP legacy (and it is used by the Enterprise then what do you consider mainstream?

Leave a Reply

Required fields are marked *.