Part 2
As I explained in my earlier post, Accessing Apache Axis2 Web service inside a Android Application - Part 1 you can create a web service. In this post I will explain how to create a android application use this web service. In my example's web service, it can give you details about cities and places in Sri Lanka. So we will query that service to get those data,
Step 1 - Create a new android project in Eclipse.
Step 2 - Add internet permission to the Android Application by editing AndroidManifest.xml file like this.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.web.frontend.calculator" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".AndroidFrontendActivity" 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 3 - Edit the main.xml file like this.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/txtCityLongitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/txtCityLatitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/txtIMLon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/txtIMLat" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/txtIMCat" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/txtIMDes" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
Step 3 - I use KSOAP as my connection between web service and Android App. So download it from here.
kSOAP2 - An efficient, lean, Java SOAP library for constrained devices
Add it to your android project. Right click on your project go to Java Build Path > Add External Jar
and select kSOAP.
Step 4 - Edit the java activity file like this.
package org.web.frontend.calculator; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class AndroidFrontendActivity extends Activity { private String METHOD_NAME = ""; // our webservice method name private String NAMESPACE = "http://ws.travel_ceylon.web.org"; // Here package name in webservice with reverse order. private String SOAP_ACTION = NAMESPACE + METHOD_NAME; // NAMESPACE + method name private static final String URL = "http://192.168.177.130:8080/Travel_Ceylon_Central_Web_Service/services/Travel_Ceylon_Web_Service?wsdl"; // you must use ipaddress here, don’t use Hostname or localhost /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String city = "Matara"; String im = "Galle Face"; METHOD_NAME = "getLongitude_City"; try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("city", city); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); Object result = envelope.getResponse(); ((TextView) findViewById(R.id.txtCityLongitude)).setText(city + " Longitude is : " + result.toString()); } catch (Exception E) { E.printStackTrace(); ((TextView) findViewById(R.id.txtCityLongitude)).setText("ERROR:" + E.getClass().getName() + ":" + E.getMessage()); } METHOD_NAME = "getLatitude_City"; try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("city", city); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); Object result = envelope.getResponse(); ((TextView) findViewById(R.id.txtCityLatitude)).setText(city + " Latitude : " + result.toString()); } catch (Exception E) { E.printStackTrace(); ((TextView) findViewById(R.id.txtCityLatitude)).setText("ERROR:" + E.getClass().getName() + ":" + E.getMessage()); } METHOD_NAME = "getLongitude_Im_Place"; try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("place", im); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); Object result = envelope.getResponse(); ((TextView) findViewById(R.id.txtIMLon)).setText(im + " Longitude : " + result.toString()); } catch (Exception E) { E.printStackTrace(); ((TextView) findViewById(R.id.txtIMLon)).setText("ERROR:" + E.getClass().getName() + ":" + E.getMessage()); } METHOD_NAME = "getLatitude_Im_Place"; try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("place", im); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); Object result = envelope.getResponse(); ((TextView) findViewById(R.id.txtIMLat)).setText(im + " Latitude : " + result.toString()); } catch (Exception E) { E.printStackTrace(); ((TextView) findViewById(R.id.txtIMLat)).setText("ERROR:" + E.getClass().getName() + ":" + E.getMessage()); } METHOD_NAME = "getCategory_Im_Place"; try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("place", im); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); Object result = envelope.getResponse(); ((TextView) findViewById(R.id.txtIMCat)).setText(im + " Category : " + result.toString()); } catch (Exception E) { E.printStackTrace(); ((TextView) findViewById(R.id.txtIMCat)).setText("ERROR:" + E.getClass().getName() + ":" + E.getMessage()); } METHOD_NAME = "getDescription_Im_Place"; try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("place", im); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); Object result = envelope.getResponse(); ((TextView) findViewById(R.id.txtIMDes)).setText(im + " Description : " + result.toString()); } catch (Exception E) { E.printStackTrace(); ((TextView) findViewById(R.id.txtIMDes)).setText("ERROR:" + E.getClass().getName() + ":" + E.getMessage()); } } }
I will explain this code like this, Here are the variable assignments,
private String METHOD_NAME = ""; // Here you have to put your method you are calling // I am calling float getLongitude_City(String city) so I assign this variable like this. METHOD_NAME = "getLongitude_City"; private String NAMESPACE = "http://ws.travel_ceylon.web.org"; // Here package name in webservice with reverse order. private String SOAP_ACTION = NAMESPACE + METHOD_NAME; // NAMESPACE + method name private static final String URL = "http://192.168.177.130:8080/Travel_Ceylon_Central_Web_Service/services/Travel_Ceylon_Web_Service?wsdl"; // you must use ipaddress here, don’t use Hostname or localhost // That is why I used a virtual machine to have a small network inside my machine.
Next I will explain the other part of the source,
METHOD_NAME = "getLongitude_City"; try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("city", city); // Here we bind parmeters to our SOAP packet. // in my getLongitude_City"it has a argument called city of Stirng // type. This is the way I assigen value to it. SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); Object result = envelope.getResponse(); // Here we get the reposnse from the SOAP object. ((TextView) findViewById(R.id.txtCityLongitude)).setText(city + " Longitude is : " + result.toString()); } catch (Exception E) { E.printStackTrace(); ((TextView) findViewById(R.id.txtCityLongitude)).setText("ERROR:" + E.getClass().getName() + ":" + E.getMessage()); }
After doing all these editing. Go to the virtual machine and run the web service. Check the connectivity between virtual machine and your machine. Then run the Android app. then you can see these information come from the web service.
Hope this article helped you. Please put comments about my article. It will help me to improve my articles in the future.