Click here to Skip to main content
15,868,016 members
Articles / Mobile Apps / Android

Calling ASP.NET Webservice (ASMX) from an Android Application, the Simplest Way

Rate me:
Please Sign up or sign in to vote.
4.89/5 (30 votes)
15 Aug 2013CPOL5 min read 450.2K   76   100
How to call ASP.NET webservice from Android Using KSOAP library in an easy and efficient way.

Introduction

There are several tutorials already present over the internet on this topic. But while going through some of these tutorials, I realized that either they are too complicated for a layman or are not explained properly. This is an important aspect of Android apps as it can easily be used to use existing business logic in Android rather than rewriting them. Another important aspect of this application is that you can easily use a global database to store the data that can be shared by different phones as against common practice of using in built SQLLite database for Android.

Using the Code

First let us look at a simple webservice.

ASP.NET
<%@ WebService language="C#" class="MyLocal" %>
using System;
using System.Web.Services;
using System.Xml.Serialization;
public class MyLocal {
    [WebMethod]
    public int Add(int a, int b) {
        return a + b;
    } 
}

While creating this webservice, it asks for a namespace and here the namespace is www.tempura.org (pretty much the default namespace which I have not changed!).

When you open this webservice in browser, you can see a window like in the figure below:

Android-asmx/figure-1.png

I have marked it to show how to get the namespace name. It will also show the list of methods, which you cannot test. So how to know the arguments and return type? Click on the method and see the figure below:

Android-asmx/Figure2-soap_structure.png

Now you know what function you are calling, its arguments, return type and namespace (and of course URL).

For calling this method you need ksoap library, which you can download from here.

Copy the downloaded zip file in any appropriate location. This is an external jar file which you need to include in your Android project.

Start an Android project and select Android API. Right click on the project node in the workspace, properties->java build path->libraries->Add external jar.

Browse and select your ksoap jar file.

All we have to do now is to write a method which can call the web service and return the result. Remember that Android gives you an exception if you try any socket operation from main activity thread. Therefore it is better to write a separate class and isolate soap related functions.

Let us now understand the logic of this class.

Java
package my.MySOAPCallActivity.namespace; 
import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class CallSoap 
{
public final String SOAP_ACTION = "http://tempuri.org/Add";

public  final String OPERATION_NAME = "Add"; 

public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

public  final String SOAP_ADDRESS = "http://grasshoppernetwork.com/NewFile.asmx";
public CallSoap() 
{ 
}
public String Call(int a,int b)
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("a");
        pi.setValue(a);
        pi.setType(Integer.class);
        request.addProperty(pi);
        pi=new PropertyInfo();
        pi.setName("b");
        pi.setValue(b);
        pi.setType(Integer.class);
        request.addProperty(pi);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;

envelope.setOutputSoapObject(request);

HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try
{
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
}
catch (Exception exception)
{
response=exception.toString();
}
return response.toString();
}
}
  • First SOAP_ACTION = namespace as seen in figure 1+function name;
  • OPERATION_NAME = name of the web method;
  • WSDL_TARGET_NAMESPACE = namespace of the webservice;
  • SOAP_ADDRESS = absolute URL of the webservice;

SOAP works on request response pair. So first you need to build a request object which can call the web service.

Java
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

Now the operation or the method that you intend to call has some arguments which you need to attach to the request object. This is done through PropertyInfo Instance pi. The important thing to notice here is that the name that you use in setName() method must be the exact name of the property that you have seen in the figure above and in setType(), the data type of the variable must be specified. Using addProperty(), add all the arguments.

Using setValue() method, set the value to the property.

Java
PropertyInfo pi=new PropertyInfo(); 
pi.setName("a"); 
pi.setValue(a); 
pi.setType(Integer.class); 
request.addProperty(pi);

Create a serialized envelope which will be used to carry the parameters for SOAP body and call the method through HttpTransportSE method.

Now you are very much ready with techniques for calling web method and getting the result. For simplicity, we have made a simple Android GUI with two EditText and one Button.

See the main.xml code as below:

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<EditText 
        android:id="@+id/editText1"
    android:layout_width="230dp"
    android:layout_height="wrap_content" >
    <requestFocus />
</EditText> 
<EditText
    android:id="@+id/editText2"
    android:layout_width="232dp"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/button1"
    android:layout_width="229dp"
    android:layout_height="wrap_content"
    android:text="@string/btnStr" />
</LinearLayout>

We want to call the method from button click event from activity class.

See the code below:

Java
package my.MySOAPCallActivity.namespace;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SimpleAsmxSOAPCallActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b1=(Button)findViewById(R.id.button1);
       final  AlertDialog ad=new AlertDialog.Builder(this).create();
         
        b1.setOnClickListener(new OnClickListener() { 
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
CallSoap cs=new CallSoap();

try
{
EditText ed1=(EditText)findViewById(R.id.editText1);
EditText ed2=(EditText)findViewById(R.id.editText2);
int a=Integer.parseInt(ed1.getText().toString());
int b=Integer.parseInt(ed2.getText().toString());

ad.setTitle("OUTPUT OF ADD of "+a+" and "+b);

String resp=cs.Call(a, b);
ad.setMessage(resp);
}catch(Exception ex)
{
ad.setTitle("Error!");
ad.setMessage(ex.toString());
}
ad.show(); }
});
    }
}

All you do now is get the values for a and b from EditTexts and pass the values to call method. Call method returns the result of the web method.

Java
String resp=cs.Call(a, b);
ad.setMessage(resp);

Once you get this up and running, you are very much likely to get an error like android.os.NetworkOnMainThreadException.

That is because Android does not permit you to run socket related operations from main thread as we had already discussed. So you need to run a thread or create a thread from where you can perform these operations. You can easily model the CallSoap class as one implementing runnable and get the stuff. But I wanted to have the calling part as a separate entity. So I just made a separate thread class for calling the CallSoap method. It gives a nice layered implementation so that the thread that is calling the function where SOAP related activities are performed is completely different.

But now the problem is your activity thread and the network thread are in multi-thread operation and chances are your main thread ends before getting the result from the SOAP operation. So I used a primitive way of waiting for the result to arrive and then using it.

The caller class.

C#
public class Caller  extends Thread  
{
    public CallSoap cs;
    public int a,b; 

    public void run(){
        try{
            cs=new CallSoap();
            String resp=cs.Call(a, b);
            MySOAPCallActivity.rslt=resp;
        }catch(Exception ex)
        {MySOAPCallActivity.rslt=ex.toString();}    
    }
}

Modified OnClick method of the button in activity thread which calls SOAP through this simple caller class.

Java
package my.MySOAPCallActivity.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MySOAPCallActivity extends Activity {

public static String rslt="";    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b1=(Button)findViewById(R.id.button1);
        final  AlertDialog ad=new AlertDialog.Builder(this).create();

        b1.setOnClickListener(new OnClickListener() {
  
            @Override public void onClick(View arg0) {
            // TODO Auto-generated method stub 

            try
            { 
                EditText ed1=(EditText)findViewById(R.id.editText1);
                EditText ed2=(EditText)findViewById(R.id.editText2); 
                int a=Integer.parseInt(ed1.getText().toString());
                int b=Integer.parseInt(ed2.getText().toString());
                rslt="START"; 
                Caller c=new Caller(); c.a=a;
                c.b=b; c.ad=ad;
                c.join(); c.start();
                while(rslt=="START") {
                    try {
                        Thread.sleep(10); 
                    }catch(Exception ex) {
                    }
                }
                ad.setTitle("RESULT OF ADD of "+a+" and "+b);
                ad.setMessage(rslt); 
            }catch(Exception ex) {
                ad.setTitle("Error!"); ad.setMessage(ex.toString());
            }
            ad.show(); 
        } });
    }
}

Finally you get the results as shown below:

Android-asmx/fig_4.png

Points of Interest

Though returning a Complex class like Employee or Person or anything like that is not as simple as the technique explained here. They need to have another serializable class. But if you love simplicity, you can return the values of the properties of the class embedded in a single string like "Name#Age#Phone".

Where ‘#’ is a delimiter. Remember that DataReader is not serializable. So your web method must convert DataReader to string format and after receiving the result, you can separate the fields with simple string splitting method.

Update: How to Run the Service locally  

As against my understanding that it can not be done, our friend Motaz has suggested a nice tips and workaround. Including it in main article so that anyone reads finds the answer quickly.

you just need to set the ip address of the service to 10.0.2.2 and the URL of the service would be :
http://10.0.2.2/service/WSGetCustomerCountryWise.asmx[^]
where 
service is your web service alias name in the IIS server
WSGetCustomerCountryWise is the name of the web service 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Integrated Ideas
India India
gasshopper.iics is a group of like minded programmers and learners in codeproject. The basic objective is to keep in touch and be notified while a member contributes an article, to check out with technology and share what we know. We are the "students" of codeproject.

This group is managed by Rupam Das, an active author here. Other Notable members include Ranjan who extends his helping hands to invaluable number of authors in their articles and writes some great articles himself.

Rupam Das is mentor of Grasshopper Network,founder and CEO of Integrated Ideas Consultancy Services, a research consultancy firm in India. He has been part of projects in several technologies including Matlab, C#, Android, OpenCV, Drupal, Omnet++, legacy C, vb, gcc, NS-2, Arduino, Raspberry-PI. Off late he has made peace with the fact that he loves C# more than anything else but is still struck in legacy style of coding.
Rupam loves algorithm and prefers Image processing, Artificial Intelligence and Bio-medical Engineering over other technologies.

He is frustrated with his poor writing and "grammer" skills but happy that coding polishes these frustrations.
This is a Organisation

115 members

Comments and Discussions

 
Question12 errors in CallSoap.java file Pin
Member 157319298-Aug-22 23:05
Member 157319298-Aug-22 23:05 
Bugthe request value is "1" in code, but the only send a "0" Pin
jairorivera5-Sep-17 10:24
jairorivera5-Sep-17 10:24 
QuestionError: Server was unable to process request. ---> Procedure or function 'GetPOP3MobileNo' expects parameter '@Pop3UserName', which was not supplied. Pin
Member 1327277521-Jun-17 20:23
Member 1327277521-Jun-17 20:23 
Questionjust getting 0 Pin
Chad Underwood19-Feb-16 13:33
Chad Underwood19-Feb-16 13:33 
AnswerRe: just getting 0 Pin
Grasshopper.iics19-Feb-16 17:24
Grasshopper.iics19-Feb-16 17:24 
AnswerRe: just getting 0 Pin
Evan Renard25-May-17 5:30
Evan Renard25-May-17 5:30 
QuestionI am getting java.net.SocketTimedoutException Pin
eravindran31-Mar-15 16:19
eravindran31-Mar-15 16:19 
AnswerRe: I am getting java.net.SocketTimedoutException Pin
Grasshopper.iics31-Mar-15 16:43
Grasshopper.iics31-Mar-15 16:43 
GeneralRe: I am getting java.net.SocketTimedoutException Pin
eravindran31-Mar-15 18:58
eravindran31-Mar-15 18:58 
GeneralRe: I am getting java.net.SocketTimedoutException Pin
eravindran3-Apr-15 18:18
eravindran3-Apr-15 18:18 
GeneralMy vote of 5 Pin
pravin prajapati10-Feb-15 18:19
professionalpravin prajapati10-Feb-15 18:19 
BugFile is Broken Pin
<zig-zag> 25-Jan-15 11:39
professional<zig-zag> 25-Jan-15 11:39 
BugCALLING-WebService-From-Android-CODE .zip Pin
ScaSal14-Nov-14 7:19
ScaSal14-Nov-14 7:19 
QuestionGetting Network Socket Exception on following example? Pin
Mubi | www.mrmubi.com2-Aug-14 23:30
professionalMubi | www.mrmubi.com2-Aug-14 23:30 
Questionjava.lang.nullPointerExcetption Pin
Sujeet KC8-May-14 19:18
Sujeet KC8-May-14 19:18 
QuestionI'm getting error on executing Pin
Sujeet KC8-May-14 3:58
Sujeet KC8-May-14 3:58 
AnswerRe: I'm getting error on executing Pin
Sujeet KC8-May-14 19:00
Sujeet KC8-May-14 19:00 
QuestionXMLPullParser Pin
Muhammad Ahmed23-Apr-14 9:26
Muhammad Ahmed23-Apr-14 9:26 
QuestionError in calling .NET Webservice from Android. Pin
Pratik219321-Mar-14 8:15
Pratik219321-Mar-14 8:15 
Generalthanks! Pin
Member 105629472-Feb-14 22:00
Member 105629472-Feb-14 22:00 
BugCan not download code! Pin
Anders Molin27-Nov-13 5:16
professionalAnders Molin27-Nov-13 5:16 
QuestionError on httpTransport.call(SOAP_ACTION, envelope); Pin
Vedder_26-Nov-13 8:55
Vedder_26-Nov-13 8:55 
AnswerRe: Error on httpTransport.call(SOAP_ACTION, envelope); Pin
Vedder_27-Nov-13 6:40
Vedder_27-Nov-13 6:40 
Questionhaving problem with paramenter passing Pin
kunal007719-Nov-13 20:21
kunal007719-Nov-13 20:21 
AnswerRe: having problem with paramenter passing Pin
Member 1116268613-Apr-15 2:14
Member 1116268613-Apr-15 2:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.