Home > DeveloperSection > Articles > SOAP example in android

SOAP example in android

Android Android  Android Libraries 
0 Comment(s)
 974  View(s)
Rate this:

SOAP example in android

SOAP stands for Simple Object Access Protocol. SOAP is a messaging protocol that allows programs that run on disparate operating systems (such as Windows and Linux) to communicate using Hypertext Transfer Protocol (HTTP) and its Extensible Mark-up Language (XML).

SOAP can form the foundation layer of a web services protocol stack, providing a basic messaging framework for web services. This XML-based protocol consists of three parts:

·        an envelope, which defines the message structure and how to process it

·        a set of encoding rules for expressing instances of application-defined datatypes

·        a convention for representing procedure calls and responses

SOAP Building Blocks


Element                                              Description                                              Required

Envelope           Identifies the XML document as a SOAP message.                Yes

Header                                 Contains header information.                                  No

Body                          Contains call, and response information.                         Yes

Fault               Provides information about errors that occurred while            No                                                                                             processing the message.


SOAP processing model

SOAP sender – a SOAP node that transmits a SOAP message

SOAP receiver – a SOAP node that accepts a SOAP message

SOAP message path – the set of SOAP nodes through which a single SOAP message passes

Initial SOAP sender (Originator) – the SOAP sender that originates a SOAP message at the starting point of a SOAP message path

SOAP intermediary – a SOAP intermediary is both a SOAP receiver and a SOAP sender and is targetable from within a SOAP message. It processes the SOAP header blocks targeted at it and acts to forward a SOAP message towards an ultimate SOAP receiver.

Ultimate SOAP receiver – the SOAP receiver that is a final destination of a SOAP message. It is responsible for processing the contents of the SOAP body and any SOAP header blocks targeted at it. In some circumstances, a SOAP message might not reach an ultimate SOAP receiver, for example because of a problem at a SOAP intermediary. An ultimate SOAP receiver cannot also be a SOAP intermediary for the same SOAP message.

Some of the advantages of leveraging SOAP include:

·         It is platform and language independent.

·         SOAP provides simplified communications through proxies and firewalls, as mentioned above.

·         It has the ability to leverage different transport protocols, including HTTP and SMTP, as well as others.

Some disadvantages of leveraging SOAP include:


·         SOAP is typically much slower than other types of middleware standards, including CORBA. This due to the fact that SOAP uses a verbose XML format. You need to fully understand the performance limitations before building applications around SOAP.

·         SOAP is typically limited to pooling, and not event notifications, when leveraging HTTP for transport. What's more, only one client can use the services of one server in typical situations.

·         Again, when leveraging HTTP as the transport protocol, there tends to be firewall latency due to the fact that the firewall is analysing the HTTP transport. This is due to the fact that HTTP is also leveraged for Web browsing, and many firewalls do not understand the difference between the use of HTTP within a Web browser, and the use of HTTP within SOAP.

·         SOAP has different levels of support, depending upon the programming language supported. For example, SOAP support within Python and PHP is not as strong as it is within Java and .NET.

Here I am creating a sample of application using SOAP

1.      Create a project in Eclipse

2.      Add ksoap2-android-assembly-2.4-jar-with-dependencies.jar file in libs.

3.      Write click on ksoap2-android-assembly-2.4-jar-with-dependencies.jar and move to the Build Path and click on Add to Build Path.

4.      Add controls in activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"












    tools:context=".MainActivity" >






        android:hint="Enter Value"

        android:inputType="numberSigned" />







        android:text="Result" />







        android:text="  Result-: "

        android:textSize="25sp" />




5.      Now add following code

package com.example.soapsample; 

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;


import android.os.AsyncTask;

import android.os.Bundle;

import android.app.Activity;

import android.app.ProgressDialog;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;


public class MainActivity extends Activity {

            Button btnResult;

            TextView tvResult;

            EditText etValue;

            private final String NAMESPACE = "http://www.w3schools.com/webservices/";

            private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

            private final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";

            private final String METHOD_NAME = "CelsiusToFahrenheit";

            private String TAG = "PGGURU";

            private static String celcius;

            private static String fahren;

            ProgressDialog progressDialog;



            protected void onCreate(Bundle savedInstanceState) {




                        getActionBar().setTitle("SOAP Celcius to Farenheit");

                        btnResult = (Button) findViewById(R.id.btnResult);

                        tvResult = (TextView) findViewById(R.id.tvResult);

                        etValue = (EditText) findViewById(R.id.etValue);

                        progressDialog = new ProgressDialog(MainActivity.this);


                        progressDialog.setMessage("Please wait ...");

                        btnResult.setOnClickListener(new OnClickListener() {



                                    public void onClick(View v) {

                                                // TODO Auto-generated method stub

                                                if (etValue.getText().length() != 0

                                                                        &&etValue.getText().toString() != "") {

                                                            // Get the text control value

                                                            celcius = etValue.getText().toString();

                                                            // Create instance for AsyncCallWS

                                                            AsyncCallWS task = new AsyncCallWS();

                                                            // Call execute


                                                            // If text control is empty

                                                } else {


                                                                                    "Please Enter Value", Toast.LENGTH_SHORT).show();








            public boolean onCreateOptionsMenu(Menu menu) {

                        // Inflate the menu; this adds items to the action bar if it is present.

                        getMenuInflater().inflate(R.menu.main, menu);

                        return true;


            private class AsyncCallWS extends AsyncTask<String, Void, Void> {


                        protected Void doInBackground(String... params) {



                                    return null;



                        protected void onPostExecute(Void result) {


                                    tvResult.setText(fahren + "° F");




                        protected void onPreExecute() {




                        protected void onProgressUpdate(Void... values) {






            public void getFahrenheit(String celsius) {

                        // Create request

                        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                        // Property which holds input parameters

                        PropertyInfo celsiusPI = new PropertyInfo();

                        // Set Name


                        // Set Value


                        // Set dataType


                        // Add the property to request object


                        // Create envelope

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(


                        envelope.dotNet = true;

                        // Set output SOAP object


                        // Create HTTP call object

                        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                        try {

                                    // Invole web service

                                    androidHttpTransport.call(SOAP_ACTION, envelope);

                                    // Get the response

                                    SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

                                    // Assign it to fahren static variable

                                    fahren = response.toString();

                        } catch (Exception ex) {

                                    Toast.makeText(getApplicationContext(), ex.toString(),





6.      Add permission in manifest.xml file

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


Now run your application



Don't want to miss updates? Please click the below button!

Follow MindStick