You are currently browsing the tag archive for the ‘Web Service’ tag.

There are tons of articles on the internet that demonstrates how to call a web service from android code. I ll try to keep it simple so that my reader don’t get confused as i was while reading others articles on the current topic. Object of this article is a  simple application that takes text from an Android Activity, than calls a web service, pass the values to the web service, than the web service do what ever it likes to do with the received text and that’s it. When we are done with this scenario than we will make this application more complicated by receiving something from a web service as a response and do something inside our application.

I ll assume that we have a web service developed and deployed for this purpose. For this tutorial we will use the web service that i have previously developed which receives two numbers and returns their sum.

Start with a simple application. Than download the latest KSoap2 jar files from the following address.

or you can search the KSoap2 library and download it from any other location. The thing to remember is that what ever is downloaded it should be ksoap2-android-something and not just ksoap2-something.  When the jar file is downloaded. Add the jar file to the existing project. Steps are given below.

  1. Right click the project and click Properties.
  2. From the opened dialog click on Java Build Path
  3. Open the Libraries Tab
  4. Click on the Add External JARs button to the right.
  5. Select the ksoap2-android-something.jar file from the HDD and click ok.

Ksoap2 jar file addition to the application

This will add the library to the current application. now coming to some coding thingi. Add the following in the import section

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;

Application that calls a web service and and sends two numbers

Add the following code to the start of our Main Activity code, outside the onCreate() method in the section where we define our global variables.

private final String SOAP_NAMESPACE = "";
private final String SOAP_URL = "";

private final String SOAP_ACTION = "";

private final String SOAP_METHOD_NAME = "add";
 private SoapObject request;
 private PropertyInfo pi1;

private PropertyInfo pi2;

In the above section we have created variables with values providing the Namespace, the URL where our web service is hosted, the action consisting our WEB METHOD in our web service and the method name that will be called. Other than that we have created two objects of type SoapObject and PropertyInfo.

//webservice thingi start

request = new SoapObject(SOAP_NAMESPACE, SOAP_METHOD_NAME);

pi1 = new PropertyInfo();
 pi1.setValue(etno1.getText().toString());//get the string that is to be sent to the web service

pi2 = new PropertyInfo();
 pi2.setValue(etno2.getText().toString());//get the string that is to be sent to the web service

SoapSerializationEnvelope envp = new SoapSerializationEnvelope(SoapEnvelope.VER11);
 envp.dotNet = true;
 HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_URL);
 try {, envp);
response = (SoapPrimitive)envp.getResponse();
 } catch (Exception e) {
Log.i("WS Error->",e.toString());
 //webservice thingi ends

In the above code we define a request object, than we define a Property Info object, set a name for it. Assign a value to it. Set its type and add that property to the already created request object.  After that “, envp);” this is the actual statement that actually calls the web service and passes the values to it. If everything is fine, the program will execute smoothly else the program will crash here.

Issues that am unable to solve so far.

There are a number of possibilities that can cause the application to crash at this point. “response = (SoapPrimitive)envp.getResponse();” this will get the response from the web service if the web service is actually returning some value. So far am trying to tune this part. As the response is not giving me the desired returned value. Either it returns a zero, if i switch between SoapPrimitive and SoapObject than either the program crashes or returns the value the i pass first to the web service.

Note:- KSOAP2 might not be a good option if this return value issue is not solved but if we are working with a web service that only receives a value from our Android application and do something of its own like saving the received string in the database on some server, than KSOAP2 can be used as explained in the above tutorial.


The reason for writing this article is that a lot of people have published too many articles on creating web services and making it simple but non is actually simple.  Almost all of those article focus on the boring theory and functionality  of the web service and not on actually what we least require to create a simple web service. Beginners to web services like me get confused in the functionality and un necessary theory. With this confusion the actual cause of creating a very simple web service dies. I have made it very simple to create a .net web service and my readers can extend this article like instead of addition they can put in what ever functionality they want in the WebMethod to get this web service work for them according to their needs.

Some Boring Theory That We Can’t Get Rid Off

  • When ever we create a web service we need some part of it to be accessed from outside. So that we can call it and make it do something for us. to make a method accessible to the outside world we need to add [WebMethod] Attribute on top of that method.
  • The method that we need to expose to the outside world must be public.
  • The file that we need to call is the one with “.asmx” extension to execute our web service.
  • We can have a private method in our web service that can be called from with in our public method. Like we can have some classes that will have some method that will do some stuff for us. To get access to that functionality of those method. We need to call those methods from our public method by creating objects of those classes in the Service1.asmx file and calling those methods through those objects.

Now Coming To The Real Stuff.

My development environment

  • Visual Studio 2010
  • Windows 7 professional 64 bit
  • Dot net framework 3.5

Creating the Web Service

Open your Visual Studio and click New Project (Section 1 in image).  From the left side panel choose C#(Section 2 in image) , Web(Section 3 in image). From the Framework selection dropdown at the top select .Net Framework 3.5 (Section 4 in image) and from the list below that select ASP.Net Web Service Application (Section 5 in image). Give the name of the web service application (Section 6 in image)  and the location (Section 7 in image) where it will be saved.  Click OK  (Section 8 in image)to create the project.

Creating a simple .net 3.5 web service application

namespace WsMirTestWebService
 /// <summary>
 /// Summary description for Service1
 /// </summary>
 [WebService(Namespace = "")]
 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
 // [System.Web.Script.Services.ScriptService]
 public class Service1 : System.Web.Services.WebService
 public string HelloWorld()
  return "Hello World";

This is what is created for us by default. now replace the method

public string HelloWorld()
  return "Hello World";

with your own method that you want to call from outside. Lets say we want to send two numbers to the web service and get the sum

 public int add(int a,int b)
  int c = 0;
  c = a+b;
  return c;

Testing Created Web Service

Now when we run our web service, we will see Service1.asmx in brower with all our WebMethods. Currently we have only one method i.e, add.

Running our web service. Service1.asmx is displayed in browser with our WebMethod “add” on display

Clicking on the add link will take us to the second screen where we will supply two values to our WebMethod and will click the Invoke button to execute the add method.

WebMethod add executed. The method expects two values, a and b.

We provide two values 7,3 and click Invoke button. This will execute our WebMethod add and if our method is returning some value it will show us the returned value.

Returned Value of our WebMethod add.

The above link can be used to publish the created web service.

Blog Stats

  • 340,336 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 234 other followers

%d bloggers like this: