|
Blogs
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"?> 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) {
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() {You'll have to define the following constants to call the Web Service : private static final String SOAP_ACTION = "testWebServices"; 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
| |||||||||||||||||||||