Basic KSOAP Android Tutorial


This is a basic KSOAP Android tutorial - here I will show you how to get started with KSOAP on Android. As you may know, we often want to access Web services via hand-held devices, and most likely you will run into trouble parsing the WSDL and the SOAP messages. Since I come from a .NET background, once I started developing on Android, I realized how much work has been Visual Studio doing for me.

That thought took me to search for a framework or library to help me consume Web Services with Android. I ran into KSOAP2, which seemed like a good library, but unfortunately, very badly documented for most scenarios, like passing or returning complex objects, working with arrays of objects or even working with dates.



All of this I needed to find out by myself and this is why I decided to write this tutorial. So, let's begin.

Getting Started with KSOAP on Android


First things first, so you should now go ahead and download the KSOAP library from Sourceforge Google code (*UPDATE* thanks Freddy):
http://code.google.com/p/ksoap2-android/downloads/detail?name=ksoap2-android-assembly-2.4-jar-with-dependencies.jar&can=2&q=


Then copy and paste the KSOAP library in the folder where your Android project will reside. Open Eclipse, start a new Android Project, right-click on the project's name and choose Properties, like this:


The next thing you need to do is to Add the KSOAP .JAR into the Android Project:




Go ahead an press Add JARs... button. Then navigate to the folder where your KSOAP library is and select it. Once you have this done, you are ready to start working with your Web Service library.

Simple Web Service Calls with KSOAP


First, let's take a look at our web service call in Visual Studio:

[WebService(Namespace = "http://vladozver.org/")]
public class SimpleWebServices : System.Web.Services.WebService
{
      [WebMethod]
      public int GetSumOfTwoInts(int Operand1, int Operand2 )
      {
         return Operand1 + Operand2;
      }
}

The service is deployed on my local machine which is on the address: 192.168.1.3. Pay attention on the ending "/" in the Namespace.

Now, KSOAP finally.

KSOAP relies on a basic object called SoapObject. For this SoapObject, there are 3 variables that are important:
The Web Service Namespace
The Web Service Method Name
The Web Service URL

There is another extra variable which is important and is called SOAP_ACTION, but that is basically a concatenation of the Namespace and Method name:

SOAP_ACTION = NAMESPACE + METHOD_NAME;

For now we will create 3 strings for the respective variables:

String NAMESPACE = "http://vladozver.org/";
        String METHOD_NAME = "GetSumOfTwoInts";
        String SOAP_ACTION = "http://vladozver.org/GetSumOfTwoInts";
        String URL = "http://192.168.1.3/VipEvents/Services/BasicServices.asmx";

Then we will create the SoapObject:
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

Parameters in KSOAP are passed via PropertyInfo class instances, so we will create some of those:

PropertyInfo pi = new PropertyInfo();
        pi.setName("Operand1");
        pi.setValue(2);
        pi.setType(int.class);
        Request.addProperty(pi);

        PropertyInfo pi2 = new PropertyInfo();
        pi2.setName("Operand2");
        pi2.setValue(5);
        pi2.setType(int.class);
        Request.addProperty(pi2);

Then we will create another important KSOAP object, and that is Soap Envelope:

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);

Because our Web Service is .NET based, we need to set the .dotNet property to true.
Next step is to create a Transport object:

AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

Lastly, we need to invoke the web service and obtain the result:

try
            {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject response = (SoapObject)envelope.getResponse();
                int result =  Integer.parseInt(response.getProperty(0).toString());
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }


That is basically it! I hope you will find this tutorial helpful, as I see that many people are looking for a simple Getting started tutorial on KSOAP Android. For more advanced usage of KSOAP, refer to my other posts on this topic.

443 comments:

«Oldest   ‹Older   801 – 443 of 443
«Oldest ‹Older   801 – 443 of 443   Newer› Newest»

Post a Comment