Thursday, 13 June 2013

Consume .net webservice in android using ksoap2

The ksoap2-android project provides a lightweight and efficient SOAP client library for the Android platform.
Check this link
1)First download ksoap2 jar file and add that file to libs folder after that right click the ksoap file and  click on add as library option.
2)add namespaces

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
3)declare the variables for accessing webservice
 private static final String SOAP_ACTION = "";

  private static final String METHOD_NAME = "HelloWorld";
        private static final String NAMESPACE = "";
        private static final String URL = "";
       TextView webserviceTextView;
       Object response;

Here  SOAP_ACTION  is the namespace of webservice.
METHOD_NAME   is the name of method created in the webservice . here its HelloWorld.
NAMESPACE  is target namespace of .asmx file.
URL  is url of .asmx file.
4) Add Internet permission to Androidanifest.xml file.

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

5)Here we need to create a class object extending the AsyncTask class as shown below..

public void onCreate(Bundle savedInstanceState) {
        new Dowork().execute();
    public class Dowork extends AsyncTask<String,Object,Object>
        private final ProgressDialog dialogue =new ProgressDialog(second.this);
        protected void onPreExecute()
        protected void onCancelled(Object result)
        protected void onPostExecute(Object result)
            if (this.dialogue.isShowing()) {

        protected Object doInBackground(String ...params)
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                AndroidHttpTransport aht=new AndroidHttpTransport(URL);
                return response;
            catch (Exception ex)
            return response;


6)this DoWork class will return the webservice response. Here in AsyncTask<String,Object,Object>
First parameter string is passed to the doInBackground() function as an array.
The second parameter is used fpr progress updates.
The third parameter is for the results. the doInBackground() return type must match the third parameter data type.
1)      Next we may want to show the progress of our task on the screen.  In this example we will use a ProgressDialog.  We will need to use the onPreExecute() method.
2)      For return to my Activity and drop off what the web search found.  Using the onPostExecute() function will can do that.
3)      Here doIn background will return response and we can perform any operation based on that result in onPostExecute () method. Here I am setting response to a textview and it will display the response from .net webservice.


  1. Have tried all the steps that you have defined. It doesnt give any error but also do not display any result.
    It only executes onpreexecute() method after that app becomes stopped.
    Any suggetions?

  2. thanks Works for me..
    HttpTransportSE aht=new HttpTransportSE (URL);

    instead of AndroidHttpTransport aht=new AndroidHttpTransport(URL);

    use SoapObject response; instead of Object response

  3. Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. Beginners Heap also provides latest tutorials of Programming from beginning to advance level.
    Be with us to learn programming in new and creative way.