Thursday 13 February 2014

How to get data from SOAP webservice object in Android

Here we shall see how to get data from SOAP web service when the response is returned as an object in XML format. When the response is object you have to create a bean class and retrieve the data. When you get single values for each variable use this post. we will also see how to parse XML data from .NET web service soap object in android.

Check this post if your service is in JSON format.

And when you get many values for single variable(Eg. Name= yashwanth, Prabhakar, Abdul, John; Id=1,2,3,4; Place = Hosur, bang, trichy, chennai ) check this post.







Step 1: Create a new project in Eclipse IDE File ⇒ New ⇒ Android Application Project and fill all the required details.

Step 2: In your layout file place some text fields and align is as required.

Activity_main.xml:


<RelativeLayout 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: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" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="97dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="24dp"
android:text="Phone"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="26dp"
android:text="Place"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="20dp"
android:text="Age"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_centerHorizontal="true"
android:text=""

android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/name"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/palce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_alignLeft="@+id/phone"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Step 3: Add Internet Permission in your project manifest file .

AndroidManifest.xml :


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exam.webservice1"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.exam.webservice1.MainActivity"
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>

Step 4: Include the KSOAP2 jar file in to your project. Right click project ⇒ Properties ⇒ Java build path ⇒ Libraries Tab ⇒ Add External Jars ⇒ browse and add KSOAP2 Jar file.

Step 5: Now crate a new class in your existing package and name that class as Bean. Now paste the below code in your bean class file.

Bean.java :


package com.exam.webservice1;

import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;


public class Bean implements KvmSerializable
{
public String Name;
public String Place;
public String Age;
public String Phone;

public Bean(){}

public Bean( String Name,String Place,String Age,String Phone)
{
this.Name=Name;
this.Place=Place;
this.Age=Age;
this.Phone=Phone;
}

public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return Name;
case 1:
return Place;
case 2:
return Age;
case 3:
return Phone;
}
return null;
}

public int getPropertyCount() {
return 4;
}

@SuppressWarnings("rawtypes")
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch(index)
{
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Name";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Place";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Age";
break;
case 3:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Phone";
break;
default:
break;
}
}

public void setProperty(int index, Object value) {
switch(index)
{
case 0:
Name = value.toString();
break;
case 1:
Place = value.toString();
break;
case 2:
Age = value.toString();
break;
case 3:
Phone = value.toString();
break;
default:
break;
}
}
}

Step 6: Now call the web service and get details with asynctask. Paste this below code in your MainActivity class file.

MainActivity.java :


package com.exam.webservice1;

import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

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

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

boolean timeoutexcep=false,httpexcep=false,generalexcep=false;
String UserName="yuki",Id="y475";

String name,place,age,phone;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new Persondetails().execute();
}

@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;
}

class Persondetails extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

@Override
protected void onPreExecute() {
this.dialog.setMessage("Loading data");
this.dialog.show();
}


@Override
protected Void doInBackground(Void... unused) {

final String NAMESPACE = "http://tempuri.org/";
final String URL = "http://192.168.1.1/Java/Service1.asmx";
final String SOAP_ACTION = "http://tempuri.org/CheckDetails";
final String METHOD_NAME = "CheckDetails";

SoapObject request2 = new SoapObject(NAMESPACE, METHOD_NAME);

request2.addProperty("sUserName", UserName);
request2.addProperty("sId", Id);


Bean C = new Bean();
PropertyInfo pi = new PropertyInfo();
pi.setName("Bean");
pi.setValue(C);
pi.setType(C.getClass());
request2.addProperty(pi);
SoapSerializationEnvelope envelope2 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope2.dotNet = true;
envelope2.setOutputSoapObject(request2);
envelope2.addMapping(NAMESPACE, "Bean", new Bean().getClass());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;

try {
androidHttpTransport.call(SOAP_ACTION, envelope2);
SoapObject response2 = (SoapObject) envelope2.getResponse();


System.out.println("check Request" + request2);
System.out.println("check response" + response2);

envelope2.addMapping(NAMESPACE, "Panchayat",new Bean().getClass());
androidHttpTransport.call(SOAP_ACTION, envelope2);
Bean[] personobj = new Bean[response2.getPropertyCount()];
Bean beanobj = new Bean();

for (int j = 0; j < personobj.length; j++) {
SoapObject pii = (SoapObject) response2.getProperty(j);
beanobj.Name = pii.getProperty(1).toString();
beanobj.Place = pii.getProperty(2).toString();
beanobj.Age = pii.getProperty(3).toString();
beanobj.Phone = pii.getProperty(4).toString();

personobj[j] = beanobj;
}

name=beanobj.Name;
age=beanobj.Place;
place=beanobj.Age;
phone=beanobj.Phone;

}
catch(SocketTimeoutException e){
timeoutexcep=true;
e.printStackTrace();
}
catch(ConnectException e){
httpexcep=true;
e.printStackTrace();
}
catch (Exception e) {
generalexcep=true;
e.printStackTrace();
}
return null;

}
@Override
protected void onPostExecute(Void result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}

if(timeoutexcep){
Toast.makeText(MainActivity.this, "Unable to connect to server, Please try again later",Toast.LENGTH_LONG).show();
}
else if(httpexcep){
Toast.makeText(MainActivity.this, "Please check your Internet connection",Toast.LENGTH_LONG).show();
}
else if(generalexcep){
Toast.makeText(MainActivity.this, "Please try later",Toast.LENGTH_LONG).show();
}

else {
display();
}
timeoutexcep=false;httpexcep=false;generalexcep=false;
}

private void display() {

TextView t1=(TextView)findViewById(R.id.name);
t1.setText(name);
TextView t2=(TextView)findViewById(R.id.palce);
t2.setText(place);
TextView t3=(TextView)findViewById(R.id.age);
t3.setText(age);
TextView t4=(TextView)findViewById(R.id.phone);
t4.setText(phone);
}
}
}



2 comments: