How to Consume Web Services from Android Applications Using kSOAP2


Android API does not provide support for Web services. Thus, Web services are consumed using a third-party libraries. One example is kSOAP2 (, optimized for Android.

The libraries need to be added to the project in order to be used. This library is based on SOAP and there is no need to generate a proxy/stub to call Web services methods.

Here is an exemple of a simple call to a method provided by a Web service:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
SoapObject request = new SoapObject(NAMESPACE, GET_INTREBARE);
// add paramaters and values
request.addProperty("idTest", idTest);
request.addProperty("idIntrebare", idIntrebare);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
//Web method call
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_WS); + GET_INTREBARE, envelope);
//get the response
SoapObject result = (SoapObject) envelope.getResponse();
//the response object can be retrieved by its name: result.getProperty("objectName")
catch (Exception e)

Some explanations of the code:

  • A SOAP envelope is created using the SoapSerializationEnvelope class (specifying the SOAP version) and the request details are added to the envelope body (using SoapObject class).
  • The HttpTransportSE class is used to make the actual call of the Web service method, the envelope being passed as parameter. The result is retrieved from the response part of the envelope.
  • It is very important to send the right parameters to the methods. In this example, the parameters used in these calls are initialized as follows:
    • NAMESPACE = “”; – Web service namespace; in this example the default namespace is used
    • URL_WS = “http://server/Service.asmx”; – Web service URL
    • GET_INTREBARE = “GetIntrebare”; – Web service method name
    • idTest, idIntrebare – the parameters used by GetIntrebare method

Using wrong values for these parameters will lead to Web service method call to fail.

In order to use Web services the application needs special permission. Permissions are stored in AndroidManifest.xml file. For this mobile application, the following line has to be added:

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


  1. #1 by Raghav Rajagopalan on March 30th, 2011


    Your post was really useful.
    But i get few errors. Can u pls tell me how to solve them
    Error is:
    Error:expected: END_TAG{}Body(position:END_TAG @1:716 in
    I have a doubt should i do some changes in androidmanifest file other than “”. If “yes” please let me know what are the changes i have to.
    Waiting for your reply.

    Thanks in advance

    Raghav Rajagopalan

    • #2 by paul.pocatilu on April 1st, 2011

      Thank you for your interest in our blog!

      There are many possible cause for this. One cause of that error could be related to data serialization (parameters or response): the types are complex and cannot be serializable by default. Another cause could be the use of wrong parameters on kSOAP objects’ methods. Also, the use of localhost instead of an IP could be the problem.

      If you don’t use android.permission.INTERNET in your android manifest file, you’ll get a Access Denied exception.

      Could you post a sample of your code if you don’t manage to solve the error?



  2. #3 by Nobin on August 31st, 2011

    I am trying to connect to a SQL Server using a .Net webservice…

    But getting permission denied error on code..

    I have given Internet permission also..

    Any help?

    My Code

    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 = “DBSelect”;
    private static final String Add_SOAP_ACTION = “”;//”http://localhost/TestWebService/Add”;
    private static final String METHOD_NAME2 = “test”;

    public void onCreate(Bundle savedInstanceState) {
    Log.v(“Start”, “App started”);
    String rec = setSomething(“Update Names set name = ‘Done’”);
    TextView tv = new TextView(this);
    // tv.animate();
    tv.setText(“Hello Android, my output is ” + rec);
    Log.v(“Start”, “App crossed writing the File–” + rec);

    // Methods to set and get data throug .Net Webservice

    public String setSomething(String qry) {

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);

    //request.addProperty(“updateStmt”,qry); // Variable Name and Value

    // get SOAP Variable Name from the WSDL file
    SoapSerializationEnvelope envelope =
    new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;

    //HtpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    java.lang.String receivedString = “1″;

    Log.v(“setSomething ()”, “Calling fn”);, envelope);
    Log.v(“setSomething ()”, “Try receiving String”);
    receivedString = (String)envelope.getResponse();
    Log.v(“setSomething ()”, “received string” + receivedString);
    // testing the App
    catch(Exception e)
    Log.v(“setSomething exception”,e.getMessage());
    return receivedString;

    • #4 by Paul on September 1st, 2011

      I have tried your code with my Web service and everything is working. Check again the permissions. This is my AndroidManifest.xml file:

  3. #5 by Rajnikant on March 20th, 2012

    Thanks for the post.
    I have question
    I have developed my wcfservice in
    Now I want to consume it in android.
    I can do it with ksoap library easily, working fine with simple data.

    Now I have Wcf Service method which returns a Person object which is created in .net.

    Now I want to get that Person into android.
    How can I do that ?

    Note : I can achieve it by using GetProperty method, but I want direct assignment.


    • #6 by Paul on March 24th, 2012

      Hi Rajnikant,

      As far as I know that is the only way to retrieve complex objects through a SoapObject. You can wrap the call in your own method that creates a Person object by using several GetProperty() calls.


  4. #7 by sumadhi on March 28th, 2012

    iam developing andriod application to consume webservices which is written in .net

    my code:
    private static final String SOAP_ACTION = “”;

    boolean result=false;
    private static final String METHOD_NAME = “HelloTo”;

    private static final String NAMESPACE = “”;

    private static final String URL = “”;

    public boolean call()
    Toast.makeText(getBaseContext(),”method called”, Toast.LENGTH_SHORT).show();

    Toast.makeText(getBaseContext(),”in try”, Toast.LENGTH_SHORT).show();
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    //request.addAttribute(“str”, phone);
    //EditText mob=EditText(findViewById(id))
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    Toast.makeText(getBaseContext(),”request1″, Toast.LENGTH_SHORT).show();

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
    Toast.makeText(getBaseContext(),”request2″, Toast.LENGTH_SHORT).show();, envelope);
    SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
    Toast.makeText(getBaseContext(),”request3″, Toast.LENGTH_SHORT).show();
    Log.i(“myApp”, response.toString());
    System.out.println(“response” +response);

    result = true;


    }catch(SocketException ex)
    //Log.e(“Error : ” , “Error on soapPrimitiveData() ” + ex.getMessage());
    catch (Exception e) {
    Log.e(“Error : ” , “Error on soapPrimitiveData() ” + e.getMessage());
    return result;

    my logcat says:

    03-28 12:05:39.732: E/Error :( 1161): Error on soapPrimitiveData() attr value delimiter missing! (position:START_TAG @2:11 in
    followed by xmlpullparser exception
    i want to run on local host.

    please suggest

  5. #8 by rathu on April 26th, 2012

    Hi Paul,
    I can able to retrieve data from Webservice. But i am not able to write complex data types like date,float etc into the webservice. I am not able to parse the date and float values. So plz help me out.

  6. #9 by rathu on April 26th, 2012

    I hav tryd with kvmserializable still the error persists.

  7. #10 by ALI HABIB on May 2nd, 2012

    how to call function in webservice that call function take parameters

  8. #11 by hckr on July 31st, 2012

    do i need WTP plugin?

  9. #12 by Chitra on November 28th, 2012

    thanks Paul for answering Nobin’s question it helped me.

  10. #13 by Sayo on August 30th, 2013

    I am using soap object only…
    and eveything is working f9 if i use API 8…
    but if i swich API to 9 to higher this doesnt work…
    Plz need help regarding this..

(will not be published)

  1. No trackbacks yet.