Friday 14 February 2014

How to get data from SOAP web service as object contains array of values

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.

And when you get single value for variables (Eg. Name= yashwanth; Age=25; place=Hosur) Check this link.









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:


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

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TableLayout
android:id="@+id/tl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TableRow
android:id="@+id/tabrow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_weight="1"
android:textAlignment="center"
android:text="@string/Id"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#f2c21b" />

<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#f2c21b" />

<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/Place"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#f2c21b" />

<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/District"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#f2c21b" />

</TableRow>
</TableLayout>

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-100dp"
android:scrollbars="vertical" >

<TableLayout
android:id="@+id/tl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</TableLayout>
</ScrollView>
</LinearLayout>
</HorizontalScrollView>

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.webservice2"
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.webservice2.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 you can find it Here.

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.webservice2;

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

public class Bean implements KvmSerializable
{
public String Id;
public String Name;
public String Place;
public String District;

public Bean(){}

public Bean(String Id,String Name, String Place,String District)
{
this.Id = Id;
this.Name=Name;
this.Place=Place;
this.District=District;
}

public Object getProperty(int arg0) {

switch(arg0)
{
case 0:
return Id;
case 1:
return Name;
case 2:
return Place;
case 3:
return District;
}
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 = "Id";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Name";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Place";
break;
case 3:
info.type = PropertyInfo.STRING_CLASS;
info.name = "District";
break;

default:
break;
}
}

public void setProperty(int index, Object value) {
switch(index)
{
case 0:
Id = value.toString();
break;
case 1:
Name = value.toString();
break;
case 2:
Place = value.toString();
break;
case 3:
District = 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.webservice2;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.Arrays;

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.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

String typ="P",uu="500229";
boolean timeoutexcep=false,httpexcep=false,generalexcep=false;
Bean[] personList2;
TableLayout ll;
String[] Id,Name,District,Place;
int i;

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

new propdetail().execute();
}

class propdetail 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.162.1.10/Test/Service.asmx";
final String SOAP_ACTION = "http://tempuri.org/GetDetails";
final String METHOD_NAME = "GetDetails";

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

request.addProperty("sTaxType", typ);
request.addProperty("sUserId", uu);

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

try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();

Log.i("myApp", request.toString());
System.out.println("check dddddddddddddd" + response);

envelope.addMapping(NAMESPACE, "Panchayat",new Bean().getClass());
androidHttpTransport.call(SOAP_ACTION, envelope);
personList2 = new Bean[response.getPropertyCount()];

for (int j = 0; j < personList2.length; j++) {
SoapObject so = (SoapObject) response.getProperty(j);
Bean person2 = new Bean();
person2.Id = so.getProperty(0).toString();
person2.Name= so.getPropertyAsString(1).toString();
person2.Place=so.getPropertyAsString(6).toString();
person2.District=so.getPropertyAsString(7).toString();
personList2[j] = person2;
}

Id = new String[personList2.length];
Name = new String[personList2.length];
Place = new String[personList2.length];
District = new String[personList2.length];

for (int i = 0; i < personList2.length; i++)
{
Place[i] = Arrays.asList(personList2[i].Id).toString();
Id[i] = Arrays.asList(personList2[i].Name).toString();
Name[i] = Arrays.asList(personList2[i].Place).toString();
District[i] = Arrays.asList(personList2[i].District).toString();

System.out.println(Id[i]);
System.out.println(Name[i]);
System.out.println(District[i]);
System.out.println(Place[i]);
}
}
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 {
tableview();
}
timeoutexcep=false;httpexcep=false;generalexcep=false;
}
}
public void tableview(){

ll = (TableLayout) findViewById(R.id.tl);
for (i = 0; i < personList2.length; i++)
{
final TableRow tr = new TableRow(this);
LayoutParams lp = new LayoutParams(150,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

tr.setId(i);

tr.setLayoutParams(lp);
lp.setMargins(0, 20, 0, 0);

final TextView tvLeft = new TextView(this);
tvLeft.setLayoutParams(lp);
tvLeft.setText(Id[i]);

final TextView tvCenter = new TextView(this);
tvCenter.setLayoutParams(lp);
tvCenter.setText(Name[i]);

final TextView tvRight = new TextView(this);
tvRight.setLayoutParams(lp);
tvRight.setText(Place[i]);

final TextView tvend = new TextView(this);
tvend.setLayoutParams(lp);
tvend.setText(District[i]);

tr.addView(tvLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
tr.addView(tvend);

ll.addView(tr, new TableLayout.LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}

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

3 comments:

  1. if i have a classe for exmple Info that has a class User in Info class and i wnat to send Info as input what am i doing?

    ReplyDelete
  2. Such a good example and very useful for me...I also want to this type of data.
    At webservice in .net how to return data

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete