So recently I have been doing a lot of Android development and generally having a great time. In general the Android API is very complete and offeres support for a whole lot of features and task. I did however have to find out that SOAP support is generally non existant. Given the widely known problems with SOAP and the trend towards simpler REST based services and the young age of the Android platform this is not too surprising. However in reality lots people still need to work with (legacy?) SOAP services. If you are in that situation you should read on.
When you research on the web for SOAP support libraries on embedded devices including Android you end up running into KSOAP and KSOAP2. Coming from a J2ME background this library is used on RIM Blackberry, other J2ME devices and now Android. For Android specifically there is a little project that converted the build to Maven and applied some patches for Android. It seems to be the preferred goto resource for KSOAP2 and Android. Unfortunately everything around KSOAP seems to be rather old and inactive. The project has a few issues assigned and had no activity for a while. But if you can read the code you can fix it to. In particular I found that xml attributes are not supported for normal parsing when no automatic marshalling is used. This has been noticed by people before and supposedly a patch to fix it has been applied. Problem is it did not do the trick for me and others. So I went ahead and fixed the issue. In the following I will show you how to get the patched source code, build it and use it in a Android project built with Maven as well as a regular Android project.
Ok I assume you still have not lost me in the technicalities so I am going to dive right in. Let’s go and get the source from my git fork on github. You can either just download a tar.gz or zip file with the source from the site directly or clone it onto your computer with git
git clone git://github.com/mosabua/ksoap2-android.git
Once you got the source you need to build it following the simple Maven convention.
cd ksoap2-android mvn clean install
This will take more or less time depending on your Maven version as well as what dependencies you have already downloaded into your local Maven repository. For me it just took 9 seconds.
Now provided you are building your Android project with Maven your got all necessary dependencies already in your repository thanks to the build you just did. So you just need to add the dependency to your Android project pom.xml file.
<dependency> <groupId>com.google.code.ksoap2-android</groupId> <artifactId>ksoap2-android</artifactId> <version>2.5-SNAPSHOT</version> </dependency>
If you are however building your project with ant or eclipse you need to take the assembly file ksoap2-android/ksoap2-assembly/target/ksoap2-android-assembly-2.5-SNAPSHOT-jar-with-dependencies.jar and put it to the a newly created libs folder inside your project. Then you will have to add it to the classpath in Eclipse. If you need more info, read more about adding external libraries and building with ant on the Android developer site. Oh and if you really dont want to build the library yourself you can download it as from this site as well.
After you have added the dependency to your project and setup your request and start parsing your response you will notice that in contrary to the prior code the getAttribute method will actually return the attributes as existant in the response xml. You will be able to do all the usual parsing you already do with properties retrieved with getProperty.
Ideally you would not have to use my fork or even build the library yourself. Unfortunately I seem to be unable to receive feedback from the project owner on the Google Code site. If you are Karl M Davies… please contact me. It would be great to see my patch applied to the main branch and maybe even see the artifacts deployed to Maven central. But that is another story…
UPDATE (2010-10-20): I have taken over the ksoap2-android project on google code as project owner and cut a new release that contains this and other improvements. More details are on my announcement blog post.