RSS

Connecting to .NET web service from Android

27 May

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:

In order to use this Web Service with our android app:

  1. Open the java file from where you would like to access the Web Service
  2. Include the class library for ksoap2

    import org.ksoap2.*;
    import org.ksoap2.serialization.*;
    import org.ksoap2.transport.*;
    import org.w3c.dom.Text;

  3. 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”;

  4. 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)
    {
    }

    }

  5. 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)
    {
    }

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

About these ads
 
12 Comments

Posted by on May 27, 2011 in Android, Mobile Development

 

12 responses to “Connecting to .NET web service from Android

  1. Ibraheem Osama Mohamed

    December 14, 2011 at 8:30 PM

    Thanks :D nice blog

     
  2. Linh Le Thai

    February 4, 2012 at 11:27 AM

    i have a NetworkOnMainThread Exception, please help me how to solve it. Thank you so much!

     
    • sochinda

      February 4, 2012 at 2:48 PM

      Hi,
      I don’t know what is your real problem, for my experienced in .NET Webservice and Android,
      I suggest you to use WCF RESTful is better than .NET Webservice, but if you still want to use .NET Webserivce i’ll send you a
      sample code to consume Web serivce in Android

       
  3. TempKaz

    March 12, 2012 at 5:54 PM

    Please send a sample code to consume a .NET SOAP web service in android to temp2005007@hotmail.com

    Thanks

     
  4. sumadhi

    March 28, 2012 at 12:52 PM

    hi…
    nice blog..
    what does this error mean
    03-28 10:21:21.693: E/Error :(626): Error on soapPrimitiveData() attr value delimiter missing! (position:START_TAG @2:11 in java.io.InputStreamReader@44c2b848)
    iam getting this in my logcat

     
  5. viola

    May 8, 2012 at 8:52 PM

    I tried this code and this error occurs to me
    The method setName(String) is undefined for the type PropertyInfo
    PropertyInfo num1 = new PropertyInfo();
    num1.setName(“a”);
    num1.setValue(5);
    request.addProperty(num1);
    please help

     
    • maddyhacking

      September 7, 2013 at 3:51 PM

      use this to set the type ….
      num1.setType(Integer.class);

       
  6. kelvin

    June 21, 2012 at 8:05 AM

    Please send a sample code to consume a .NET SOAP web service in android to
    kelvinbiffi@hotmail.com

     
  7. Juande

    July 14, 2012 at 6:45 AM

    Please send me a sample code too, i need to consume an .asmx web service from android app.
    Please, Thanks in advance. stad_comp@yahoo.com

     
  8. Benny

    December 6, 2012 at 1:01 PM

    i founded this tool to auto generate wsdl to android code,

    http://www.wsdl2code.com/Example.aspx

    public void callWebService(){
    SampleService srv1 = new SampleService();
    Request req = new Request();
    req.companyId = “1”;
    req.userName = “userName”;
    req.password = “pas”;
    Response response = srv1.ServiceSample(req);
    }

     
  9. whatsapp

    April 22, 2013 at 3:58 PM

    I got this site from my friend who told me concerning this website and at the moment this time I am visiting this
    web page and reading very informative content here.

     
  10. Alfred

    June 25, 2014 at 5:40 AM

    Hi there, I read your blog on a regular basis.
    Your story-telling style is witty, keep up the good work!

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: