Connecting to .NET web service from Android
If you are having trouble using .NET Web Services with the Android Platform, you have probably reached to the solution here.
I am here demonstrating the steps using which you can 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:
I am here demonstrating the steps using which you can 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
- 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)
{
}
}
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)
{
}
} - 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)
{
}
No comments:
Post a Comment