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.  http://mirnauman.wordpress.com/2012/08/08/creating-a-simple-dot-net-web-service/

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

http://code.google.com/p/ksoap2-android/downloads/detail?name=ksoap2-android-assembly-2.4-jar-with-dependencies.jar&can=2&q=

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 = "http://tempuri.org/";
private final String SOAP_URL = "http://10.52.0.114:8082/Service1.asmx";

private final String SOAP_ACTION = "http://tempuri.org/add";

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.setName("a");
 pi1.setValue(etno1.getText().toString());//get the string that is to be sent to the web service
 pi1.setType(String.class);
request.addProperty(pi1);

pi2 = new PropertyInfo();
 pi2.setName("b");
 pi2.setValue(etno2.getText().toString());//get the string that is to be sent to the web service
 pi2.setType(String.class);
request.addProperty(pi2);

SoapSerializationEnvelope envp = new SoapSerializationEnvelope(SoapEnvelope.VER11);
 envp.dotNet = true;
 envp.setOutputSoapObject(request);
 HttpTransportSE androidHttpTransport = new HttpTransportSE(SOAP_URL);
 try {
 androidHttpTransport.call(SOAP_ACTION, 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 “androidHttpTransport.call(SOAP_ACTION, 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.

About these ads