Tuesday, 16 August 2011

Consuming .net Web Service from Android App using KSOAP

Here in this post im demonstrating steps to consume data from the .NET web service in your android app.

 In order to use .Net web Service from your android application you need to first download the ksoap2 android API. Follow the link to download ksoap2 API for android.

After downloading the API, extract the zip file to the file system. Open your Android Project and navigate to the Project Properties. In the project properties go to Java Build Path and say Add External JARs.

Add the reference to the extracted ksoap2-j2se-full-2.1.2.jar file from the downloaded API. You are now ready to use ksoap2 to connect to the .NET web service via Android.

Let’s assume a .NET web service with two methods “Hello World” that returns a string and “Add” that accepts two numbers and returns their sum. Following is the WSDL file of the web service.

From the above WSDL file we get the following Information about the web service:

NameSpace: http://localhost/TestWebService/
Web Service URl: http://TestServer/Test/Service.asmx
Method “Hello World” SoapAction URL: http://localhost/TestWebService/HelloWorld
Method “Hello World” Output Type: String
Method “Add” SoapAction URL: http://localhost/TestWebService/Add
Method Hello World Input Type: Int, Int
Method Hello World Output Type: Int

In order to use this Web Service with our android app:
Open the java file from where you would like to access the Web Service

Include the class library for ksoap2
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import org.w3c.dom.Text;

Define Web Service Properties in the class

private static final String NAMESPACE = “http://localhost/TestWebService/” ;
private static final String URL = ” http://TestServer/Test/service.asmx”;

private static final String HelloWorld_SOAP_ACTION = “http://localhost/TestWebService/HelloWorld”;
private static final String METHOD_NAME1 = “HelloWorld”;
private static final String Add_SOAP_ACTION = “http://localhost/TestWebService/Add”;
private static final String METHOD_NAME2 = “Add”;

Add methods to call the web service methods and retrieve the results

public void GetHelloWorld() {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try
{
androidHttpTransport.call(HelloWorld_SOAP_ACTION, envelope);
java.lang.String receivedString = (String)envelope.getResponse();
}
catch(Exception e)
{
e.printStackTrace();
}
}

public void GetAdd() {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);

PropertyInfo num1 = new PropertyInfo();
num1.setName(“a”);
num1.setValue(5);
request.addProperty(num1);
PropertyInfo num2 = new PropertyInfo();
num2.setName(“b”);
num2.setValue(9);
request.addProperty(num2);

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

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try
{
androidHttpTransport.call(Add_SOAP_ACTION, envelope);
java.lang.Integer receivedInt = (Integer)envelope.getResponse();
}
catch(Exception e)
{
e.printStackTrace();
}
}

If you app require access to an array, you can use the following code:

ArrayList<String> a = new ArrayList<String>();
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
java.util.Vector<Object> receivedStrings = (java.util.Vector<Object>)envelope.getResponse();
if(receivedStrings != null)
{
for(Object curStrings : receivedStrings)
{
a.add(curStrings.toString());
}
}
}
catch(Exception e)
{
e.printStackTrace();
}

Referenced by: http://composedcrap.blogspot.com/2009/08/connecting-to-net-web-service-from.html 

1 comment:

  1. i want to send a image as base64string to the webservice.but iam getting exception:
    01-04 13:20:27.289: E/Error :(1489): Error System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ArgumentNullException: Value cannot be null.
    01-04 13:20:27.289: E/Error :(1489): Parameter name: InString
    01-04 13:20:27.289: E/Error :(1489): at System.Convert.FromBase64String(String s)
    01-04 13:20:27.289: E/Error :(1489): at WebServiceDemo.InsertImage(String name) in c:\Documents and Settings\admin\My Documents\Visual Studio 2008\WebSites\Webservicedemo\webservicedemo.asmx:line 214
    01-04 13:20:27.289: E/Error :(1489): --- End of inner exception stack trace ---
    please suggest

    ReplyDelete