Archive

Posts Tagged ‘Android Web Services’

Android Web Services Using KSOAP2

August 13, 2010 Leave a comment

While working on a new mobile application for one of my clients, I had to make calls out to web services to retrieve data.  Using the Android framework to make these calls was very cumbersome.

After doing a little Googling I found a nifty library to import into my application that made web service calls for the Droid simplistic.  KSOAP2, a free library to download, made calling SOAP web services a breeze.  KSOAP2 can be downloaded by clicking the following link.

        Click Here To Download KSOAP

Once you have KSOAP2 downloaded I’ll go through two examples of using KSOAP2.  The first example will use primitive data types.  The second example will use complex data types and objects from the WSDL.

For the primitive example we are going to call a fictions web service http://www.hypersonictechnologies.com/example.asmx, and call the HelloWorld web method. This method will return back "Hello World".
For both examples we need to modify our manifest file to allow the Internet permission.  The Internet permission is a must to allow your Android application to access websites and services.  Start by adding the following line to your manifest file.

<uses-permission android:name="android.permission.INTERNET" />

Next we are going to create an activity Primitive.java. Inside the Primitive activity you will need to import the following classes.

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

Next we are going to add some local variables to the activity.

private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String SOAP_METHOD = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.hypersonictechnologies.com/Example.asmx";

SOAP_ACTION is the namespace of our web service and the name of the method we are going to call.
SOAP_Method is the name of the web method we are going to call.
NAMESPACE is the the namespace of our web service. This can be found in the attributes of your web service. By default if you are using Microsoft Visual Studio http://tempuri.org/ will be the default.
URL is the address to the windows web service.

Now that we have our variables setup, we can now make a call to HelloWorld and receive data. I’m going to create a function in our activity called GetData.

private string GetData()
{
    	SoapObject request = new SoapObject(NAMESPACE, SOAP_METHOD);    	    	
    	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    	envelope.dotNet = true;
    	envelope.setOutputSoapObject(request);
    	
    	AndroidHttpTransport aht = new AndroidHttpTransport(URL);
    	@SuppressWarnings("unused")
		Object result;
    	try
    	{
    		aht.call(SOAP_ACTION, envelope);
    		result = (Object)envelope.getResponse();  		
    	} catch (Exception ex) {
    		result = null;
    	}      	    
	
	return result;
}

One thing to notice in the code is the line (envelope.dotNet = true;). You’ll want to make sure you have this line when calling Asp.Net web services. Without this line your web service will return errors, and like most errors they aren’t very descriptive.

OK calling a primitive web service is made easier now with KSOAP2, but what if you want to pass in parameters to the web service? Well for that we just add a few extra lines (1 for each parameter). We’ll use the existing function GetData, but we will make it so we pass in two parameters (string FirstName and string LastName).

private string GetData(string FirstName, string LastName)
{
    	SoapObject request = new SoapObject(NAMESPACE, SOAP_METHOD);    	
    	request.addProperty("firstname", FirstName);
	request.addProperty("lastname", LastName);
	
	... 
}

OK, now for the fun part. I don’t want to send back a primative from my web service, instead I want to send back a list, or any other object I have defined. For this example we are going to create an activity named Custom.java. In this activity we will setup the web service exactly the same as Primative.java except we do not need to import org.ksoap2.serialization.SoapPrimitive.

Since the rest of the code is the same I’m going to skip down to the GetData method of the activity. This time we are going to return a generic object. This object will contain two fields, FirstName and LastName.

private void GetData(string AccountNumber)
{
    	SoapObject request = new SoapObject(NAMESPACE, SOAP_METHOD);    	
    	request.addProperty("account", AccountNumber);
    	
    	SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    	envelope.dotNet = true;
    	envelope.setOutputSoapObject(request);
    	
    	AndroidHttpTransport aht = new AndroidHttpTransport(URL);

    	try
    	{
    		aht.call(SOAP_ACTION, envelope);

    		
    		SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;                	               
			SoapObject player = (SoapObject) resultsRequestSOAP.getProperty("GetAccountResult");

    		String fname = player.getProperty("FirstName").toString();
    		String lname = player.getProperty("LastName").toString();
			
    	} catch (Exception ex) {
    		result = null;
    	}      	    
}
Categories: Android Web Services
Follow

Get every new post delivered to your Inbox.