Tuesday, February 14, 2012

Calling a Web Service through Android

Calling a Web Service using Android
In this post we shall learn how to call a web service using Android.
For this purpose we shall use the Hypatia Quotes Web Service at
http://hypatiasoftwaresolutions.com/HypatiaQuotesService.asmx
which returns random quotes of Hypatia.

The WSDL for this Web Service is :-

http://hypatiasoftwaresolutions.com/HypatiaQuotesService.asmx?WSDL

<wsdl:definitions targetNamespace="http://tempuri.org/"><wsdl:types><s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"><s:element name="GetAQuote"><s:complexType/></s:element><s:element name="GetAQuoteResponse"><s:complexType><s:sequence><s:element minOccurs="0" maxOccurs="1" name="GetAQuoteResult" type="s:string"/></s:sequence></s:complexType></s:element></s:schema></wsdl:types><wsdl:message name="GetAQuoteSoapIn"><wsdl:part name="parameters" element="tns:GetAQuote"/></wsdl:message><wsdl:message name="GetAQuoteSoapOut"><wsdl:part name="parameters" element="tns:GetAQuoteResponse"/></wsdl:message><wsdl:portType name="HypatiaQuotesServiceSoap"><wsdl:operation name="GetAQuote"><wsdl:input message="tns:GetAQuoteSoapIn"/><wsdl:output message="tns:GetAQuoteSoapOut"/></wsdl:operation></wsdl:portType><wsdl:binding name="HypatiaQuotesServiceSoap" type="tns:HypatiaQuotesServiceSoap"><soap:binding transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="GetAQuote"><soap:operation soapAction="http://tempuri.org/GetAQuote" style="document"/><wsdl:input><soap:body use="literal"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:binding name="HypatiaQuotesServiceSoap12" type="tns:HypatiaQuotesServiceSoap"><soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="GetAQuote"><soap12:operation soapAction="http://tempuri.org/GetAQuote" style="document"/><wsdl:input><soap12:body use="literal"/></wsdl:input><wsdl:output><soap12:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="HypatiaQuotesService"><wsdl:port name="HypatiaQuotesServiceSoap" binding="tns:HypatiaQuotesServiceSoap"><soap:address location="http://hypatiasoftwaresolutions.com/HypatiaQuotesService.asmx"/></wsdl:port><wsdl:port name="HypatiaQuotesServiceSoap12" binding="tns:HypatiaQuotesServiceSoap12"><soap12:address location="http://hypatiasoftwaresolutions.com/HypatiaQuotesService.asmx"/></wsdl:port></wsdl:service></wsdl:definitions>


Out of this document two parts are very important, and I have underlined them:-
1) <soap:operation soapAction="http://tempuri.org/GetAQuote" style="document"/>and
2) <wsdl:operation name="GetAQuote">
The first one becomes the soap action, and the second becomes the method name.
Now, getting to the android part.First of all download the ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar. This file is currently available at :-

http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-
android-assembly/2.6.0/ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar
Next, add this file to the current project.
1)

2)


Here is the complete project in Eclipse:-

The Activity class in eclipse

HypatiaQuotesClientActivity

package quotes.hypatia;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class HypatiaQuotesClientActivity extends Activity  implements OnClickListener{
    /** Called when the activity is first created. */
    private Button b;
    private TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b=(Button)findViewById(R.id.button1);
        tv=(TextView)findViewById(R.id.txtQuote);
        b.setOnClickListener(this);
       
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        try
        {
        String namespace = "http://hypatiasoftwaresolutions.com";
        String url ="http://hypatiasoftwaresolutions.com/HypatiaQuotesService.asmx?WSDL";   
        String soapaction = "http://tempuri.org/GetAQuote";
        String methodname = "GetAQuote";
        SoapObject request = new SoapObject(namespace, methodname);        
        SoapSerializationEnvelope envelope =
            new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(url);

       
            androidHttpTransport.call(soapaction, envelope);
            org.ksoap2.serialization.SoapObject resultsRequestSOAP =(org.ksoap2.serialization.SoapObject) envelope.bodyIn;
            int n=resultsRequestSOAP.getPropertyCount();
            tv.setText("" + resultsRequestSOAP.getPropertyAsString(n-1));
        }
        catch(Exception ex)
        {
            tv.setText("" + ex);
        }
    }
}



main.xml

<?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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />




    <TextView
        android:id="@+id/txtQuote"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/nothing"
        android:textAppearance="?android:attr/textAppearanceLarge" />



    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/ButtonText" />

</LinearLayout>



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="quotes.hypatia"
    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:label="@string/app_name"
            android:name=".HypatiaQuotesClientActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Since, we are calling web services, the internet permission is required:-


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


strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Random Hypatia Quotes</string>
    <string name="app_name">HypatiaQuotesClient</string>
    <string name="nothing"></string>
    <string name="ButtonText">Hypatia Quotes</string>

</resources>


Here is the output of the program in the emulator:-

Sample output 1

Sample output 2


In the next post of this series, we shall call a web service that requires parameters.

The Web Service running on my Mobile.

No comments:

Post a Comment