Sunday 5 February 2012

SOAP




Simple Object Access Protocol (SOAP) Android

SOAP provides a simple and lightweight mechanism for exchanging structured and typed information between peers in a decentralized, distributed environment using XML. SOAP does not itself define any application semantics such as a programming model or implementation specific semantics; rather it defines a simple mechanism for expressing application semantics by providing a modular packaging model and encoding mechanisms for encoding data within modules. This allows SOAP to be used in a large variety of systems ranging from messaging systems to RPC.



Following SOAP Android Example :
KSOAPParserActivity.java 



package com.ksoapparser;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class KSOAPParserActivity extends Activity {

private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "Enter your URL";

private static final String SOAP_ACTION = "Enter your SOAP Action";
private static final String METHOD_NAME = "Enter your Method name";

// private static final String[] sampleACTV = new String[] { "android","iphone", "blackberry" };
String token= null,error = null,sdec = null,ldec = null;
TextView TextView01,TextView02,TextView03,TextView04;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>
// (this, android.R.layout.simple_dropdown_item_1line, sampleACTV);
//
// AutoCompleteTextView ACTV = (AutoCompleteTextView)findViewById
// (R.id.AutoCompleteTextView01);
// ACTV.setAdapter(arrAdapter);
TextView01 = (TextView) findViewById(R.id.TextView01);
TextView02 = (TextView) findViewById(R.id.TextView02);
TextView03 = (TextView) findViewById(R.id.TextView03);
TextView04 = (TextView) findViewById(R.id.TextView04);
Button button = (Button)findViewById(R.id.button1);
Button button1 = (Button)findViewById(R.id.button2);


SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("random", "Stranger");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

try {


androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
// ACTV.setHint("Received :" + resultsRequestSOAP.toString());

token  = ((SoapObject)resultsRequestSOAP.getProperty(0)).getProperty(0).toString();
Log.i("Token...", ""+ token);
error =  ((SoapObject)resultsRequestSOAP.getProperty(1)).getProperty(0).toString();
  Log.i("Code...", ""+error);

sdec  =  ((SoapObject)resultsRequestSOAP.getProperty(1)).getProperty(1).toString();
               Log.i("Short...", ""+ sdec);
       
ldec  =  ((SoapObject)resultsRequestSOAP.getProperty(1)).getProperty(2).toString();
Log.i("Long...", ""+ ldec);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView01.setText(token);
TextView02.setText(error);
TextView03.setText(sdec);
TextView04.setText(ldec);
}
});
button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView01.setText("Code");
TextView02.setText("Error");
TextView03.setText("Short");
TextView04.setText("Long");
}
});

} catch (Exception e) {
e.printStackTrace();
}
}
}

main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">

<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Code"></TextView>
<TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Error"></TextView>
<TextView android:id="@+id/TextView03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Short"></TextView>
<TextView android:id="@+id/TextView04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Long"></TextView>
<Button android:text="Set Now" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Refresh" android:id="@+id/button2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>



AndroidManifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ksoapparser"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".KSOAPParserActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>