articles

Home / DeveloperSection / Articles / SOAP example in android

SOAP example in android

Manoj Pandey3654 05-May-2015

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.

SOAP example in android


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"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_gravity="center"

    android:layout_marginBottom="60sp"

    android:gravity="center"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <EditText

        android:id="@+id/etValue"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:hint="Enter Value"

        android:inputType="numberSigned" />

 

    <Button

        android:id="@+id/btnResult"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="20sp"

        android:text="Result" />

 

    <TextView

        android:id="@+id/tvResult"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="10sp"

        android:text="  Result-: "

        android:textSize="25sp" />

 

</LinearLayout>

 

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;

 

            @Override

            protected void onCreate(Bundle savedInstanceState) {

                        super.onCreate(savedInstanceState);

 

                        setContentView(R.layout.activity_main);

                        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() {

 

                                    @Override

                                    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

                                                            task.execute();

                                                            // If text control is empty

                                                } else {

                                                            Toast.makeText(getApplicationContext(),

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

                                                }

 

                                    }

                        });

            }

 

            @Override

            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> {

                        @Override

                        protected Void doInBackground(String... params) {

 

                                    getFahrenheit(celcius);

                                    return null;

                        }

                        @Override

                        protected void onPostExecute(Void result) {

 

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

                                    progressDialog.cancel();

                        }

                        @Override

                        protected void onPreExecute() {

                                    progressDialog.show();

                        }

                        @Override

                        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

                        celsiusPI.setName("Celsius");

                        // Set Value

                        celsiusPI.setValue(celsius);

                        // Set dataType

                        celsiusPI.setType(double.class);

                        // Add the property to request object

                        request.addProperty(celsiusPI);

                        // Create envelope

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

                                                SoapEnvelope.VER11);

                        envelope.dotNet = true;

                        // Set output SOAP object

                        envelope.setOutputSoapObject(request);

                        // 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(),

                                                            Toast.LENGTH_SHORT).show();

                        }

            }

}

6.   Add permission in manifest.xml file

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

 

Now run your application

 

     SOAP example in android      SOAP example in android


Updated 07-Sep-2019

Leave Comment

Comments

Liked By