Soap in Android
In this post I'm going to illustrate how we can access web service in Android usingksoap2-android project that provides a lightweight and efficient SOAP library for the Android platform.
You can download the jar file from following link;
http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.8/ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar
After that just follow these steps:
-> Creating a folder "libs" in the project
-> Copying the external jars in to the folder
-> Refresh the folder
-> Go to properties -> Build path -> Add Jar (not external JAR)
-> Clean the project
-> Restart Eclipse
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/txt_from_currency"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="FromCurrency"/>
<EditText
android:id="@+id/txt_to_currency"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ToCurrency"/>
<Button
android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Submit"/>
<TextView
android:id="@+id/lbl_rate"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textSize="40dip" />
</LinearLayout>
Main.java
public class Main extends Activity {
private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/CurrencyConvertor.asmx";
private final String SOAP_ACTION = "http://www.webserviceX.NET/ConversionRate";
private final String METHOD_NAME = "ConversionRate";
EditText from;
EditText to;
Button btn;
TextView rate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
from = (EditText)findViewById(R.id.txt_from_currency);
to = (EditText)findViewById(R.id.txt_to_currency);
btn = (Button)findViewById(R.id.btn);
rate = (TextView)findViewById(R.id.lbl_rate);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String from_ = from.getText().toString();
String to_ = to.getText().toString();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo _from =new PropertyInfo();
_from.setName("FromCurrency");
_from.setValue(from_);
_from.setType(double.class);
request.addProperty(_from);
PropertyInfo _to =new PropertyInfo();
_to.setName("ToCurrency");
_to.setValue(to_);
_to.setType(double.class);
request.addProperty(_to);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
//SoapObject result = (SoapObject)envelope.getResponse();
rate.setText(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
You can download the jar file from following link;
http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.8/ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar
After that just follow these steps:
-> Creating a folder "libs" in the project
-> Copying the external jars in to the folder
-> Refresh the folder
-> Go to properties -> Build path -> Add Jar (not external JAR)
-> Clean the project
-> Restart Eclipse
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pxr.tutorial.soap.weather"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
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>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/txt_from_currency"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="FromCurrency"/>
<EditText
android:id="@+id/txt_to_currency"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ToCurrency"/>
<Button
android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Submit"/>
<TextView
android:id="@+id/lbl_rate"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textSize="40dip" />
</LinearLayout>
Main.java
public class Main extends Activity {
private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/CurrencyConvertor.asmx";
private final String SOAP_ACTION = "http://www.webserviceX.NET/ConversionRate";
private final String METHOD_NAME = "ConversionRate";
EditText from;
EditText to;
Button btn;
TextView rate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
from = (EditText)findViewById(R.id.txt_from_currency);
to = (EditText)findViewById(R.id.txt_to_currency);
btn = (Button)findViewById(R.id.btn);
rate = (TextView)findViewById(R.id.lbl_rate);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String from_ = from.getText().toString();
String to_ = to.getText().toString();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo _from =new PropertyInfo();
_from.setName("FromCurrency");
_from.setValue(from_);
_from.setType(double.class);
request.addProperty(_from);
PropertyInfo _to =new PropertyInfo();
_to.setName("ToCurrency");
_to.setValue(to_);
_to.setType(double.class);
request.addProperty(_to);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
//SoapObject result = (SoapObject)envelope.getResponse();
rate.setText(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
No comments:
Post a Comment