Friday, April 13, 2012

Android Web Service with .NET using SOAP

In this post I'm going to illustrate how we can access web service in Android usingksoap2-android project that provides a lightweight and efficient SOAP library for the Android platform. 

You can download the jar file from following link; 

Here is the sample code that illustrate SOAP web service call. I have access a live web service ConvertWeight from http://www.webserviceX.NET/ which convert weight from one unit to another. I have set "envelope.dotNet = true;" in Line 53 since I'm accessing .NET web service. You can comment that line if your web service is not .NET one. 

package com.sencide;  
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;  
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class AndroidWebService extends Activity {
    private final String NAMESPACE = "http://www.webserviceX.NET/";
    private final String URL = "";
    private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight";
    private final String METHOD_NAME = "ConvertWeight";
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        String weight = "3700";
        String fromUnit = "Grams";
        String toUnit = "Kilograms";
        PropertyInfo weightProp =new PropertyInfo();
        PropertyInfo fromProp =new PropertyInfo();
        PropertyInfo toProp =new PropertyInfo();
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {
  , envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            Log.i("myApp", response.toString());
            TextView tv = new TextView(this);
            tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);
        } catch (Exception e) {

You have to add INTERNET permission to AndroidManifest.xml as follows; 

<?xml version="1.0" encoding="utf-8"?>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidWebService"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

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

You can download the source code of above project (Password:sara). 

I have updated my previous code in the post Android Login Screen Using HttpClientto authenticate users from web service. There I have illustrate how you can start new activity if the login is successful. 

You can download the updated source code with sample .NET web service application (Password:sara).

No comments:

Post a Comment