Blogs

Consuming SOAP Web Services with Android
Pierre DOMINIQUE
Business Card
Company: HCA International
Posted on Oct. 30, 2009 08:05 AM in Java Programming, Mobile, Open Source, Service-Oriented Architecture

Subscribe.Subscribe
Print. Print
Permalink Permalink

This blog will show you how to consume SOAP Web Services with Android.

 

Android and SOAP Web Services

There's no standard Android API to consume SOAP Web Services so we'll have to use a third party library. kSOAP 2 is a SOAP web service client library for constrained Java environments.

kSOAP2 needs some modifications to work with Android. The instructions how to do this are explained in this blog. You can just download the ksoap2_android_src.zip  file and use it in your project.

 

A simple example

Let's create a simple application using a simple Web Service provided by SAP (GRMGWSTest). This Web Service can be used to test the J2EE Engine Web Services and you can access it via this URL : http://myserver:50000/GRMGWSTest/service/. The application will call the Web Service and display the response when the user clicks on a button.

 

1.       Install the Android SDK

I won't go through the process of installing the Android SDK, the provided documentation is clear and concise.

 

2.       Create a new Android project

Open Eclipse and create a new Android Project (File > New > Android Project). Choose a project name (i.e SOAPClient), build target (i.e Android 1.5), fill in the required properties and click on Finish :

 

Application name : SOAPClient

Package name : com.hca.examples

Create Activity : SOAPClientActivity

 

3.       Copy kSOAP 2 files in your workspace

Unzip ksoap2_android_src.zip and copy the org folder in your workspace :

 

Then refresh the project in Eclipse (F5). The src folder should now contain the new files :

 

4.       Create the layout for the main activity

First we have to define a layout for the main activity. This is a simple layout with a single button.

Open the res > layout > main.xml file and paste the following code :

 
<?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"
android:gravity="center"
>
<Button
android:id="@+id/btTest"
android:layout_width="150px"
android:layout_height="50px"
android:text="Test Web Services"
/>
</LinearLayout>
 

5.       Implement the main activity

Then we have modify the onCreate method of the main Activity to find the button we defined in the layout and set a listener for this button.

Open the SOAPClientActivity.java file and paste the following code :

 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btTest = (Button) this.findViewById(R.id.btTest);
btTest.setOnClickListener(btTestListener);
}

 

Then we have to implement the listener to call the Web Service when the user clicks on the button.

Open the SOAPClientActivity.java file and paste the following code :

 
public Button.OnClickListener btTestListener = new Button.OnClickListener() {
public void onClick(View v) {
try {
// Create SOAP request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);

// Get response from envelope
Object result = envelope.getResponse();

// Display result
Toast.makeText(SOAPClientActivity.this, result.toString(),
50000).show();

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

You'll have to define the following constants to call the Web Service :

 
private static final String SOAP_ACTION = "testWebServices";
private static final String METHOD_NAME = "testWebServices";
private static final String NAMESPACE = "urn:GRMGWSTestBeanVI";
private static final String URL = "http://myserver.com:50000/GRMGWSTest/service?wsdl&style=document";
 

6.       Add missing INTERNET permission

If you launch the application at this point, you'll get the following error :

java.net.SocketException: Permission denied (maybe missing INTERNET permission)

 

You need to modify the AndroidManifest.xml file to add the INTERNET permission :

 

Add the following line just before the </manifest> tag :

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

 

7.       Launch the application

Re-Launch the application and click on the button to display the Web Service response :

 

Final thoughts

Consuming SOAP Web Services with Android is possible but is it a good idea? Here's an interesting quote from kSOAP 2 website :

 

"Please note that SOAP introduces some significant overhead for web services that may be problematic for mobile devices. If you have full control over the client and the server, a REST based architecture may be more adequate."

 

I'll show you how to consume REST Web Services with Android in my next blog.

Pierre DOMINIQUE is a SAP NetWeaver Consultant for HCA International.


Add to: del.icio.us | Digg | Reddit


Comment on this articleComments and questions are welcome.
Comment on this weblog
Showing messages 1 through 3 of 3.

Titles Only Main Topics Oldest First


Showing messages 1 through 3 of 3.