Friday, February 17, 2012

Calling a Web Service through Android and passing Parameters

In the last post http://champakonline.blogspot.com/2012/02/calling-web-service-through-android.html , we learned how to call a .Net Web Service from Android. In this post, we shall call a Web Service that requires parameters to be sent.
For this application, we shall use the Web Service whose WSDL is at
http://hypatiasoftwaresolutions.com/HypatiaQuotesService.asmx?WSDL

The WSDL is



<wsdl:definitions targetNamespace="http://tempuri.org/"><wsdl:types><s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"><s:element name="getBirthday"><s:complexType><s:sequence><s:element minOccurs="0" maxOccurs="1" name="name" type="s:string"/></s:sequence></s:complexType></s:element><s:element name="getBirthdayResponse"><s:complexType><s:sequence><s:element minOccurs="0" maxOccurs="1" name="getBirthdayResult" type="s:string"/></s:sequence></s:complexType></s:element><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="getBirthdaySoapIn"><wsdl:part name="parameters" element="tns:getBirthday"/></wsdl:message><wsdl:message name="getBirthdaySoapOut"><wsdl:part name="parameters" element="tns:getBirthdayResponse"/></wsdl:message><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="getBirthday"><wsdl:input message="tns:getBirthdaySoapIn"/><wsdl:output message="tns:getBirthdaySoapOut"/></wsdl:operation><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="getBirthday"><soap:operation soapAction="http://tempuri.org/getBirthday" style="document"/><wsdl:input><soap:body use="literal"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation><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="getBirthday"><soap12:operation soapAction="http://tempuri.org/getBirthday" style="document"/><wsdl:input><soap12:body use="literal"/></wsdl:input><wsdl:output><soap12:body use="literal"/></wsdl:output></wsdl:operation><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>




Since we shall be calling the getBirthday method. The relevant portions to remember are :-


1) <wsdl:definitions targetNamespace="http://tempuri.org/">

2)  <soap:operation soapAction="http://tempuri.org/getBirthday" style="document"/>

3)  <s:sequence><s:element minOccurs="0" maxOccurs="1" name="name" type="s:string"/></s:sequence>

The first one gives the namespace to be used, the second is the soap action, and the third is the parameter to be passed. The name of the parameter is "name" while the type is "s:string".

It is important to note that, should any of these three items be used wrongly, the parameter passing and calling would not work.


Here is the code for the activity:-

HypatiaFriendsActivity.java

package hypatia.friends;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class HypatiaFriendsActivity extends Activity  implements OnClickListener{
    /** Called when the activity is first created. */
    private Button b;
    private TextView tv;
    private EditText txtname;
    private TextView txtBirthday;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b=(Button)findViewById(R.id.button1);
        tv=(TextView)findViewById(R.id.txtName);
        txtBirthday=(TextView)findViewById(R.id.txtBirthday);
        txtname=(EditText)findViewById(R.id.txtName);
        b.setOnClickListener(this);
       
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        try
        {
        String namespace = "http://tempuri.org/";
        String url ="http://hypatiasoftwaresolutions.com/HypatiaQuotesService.asmx?WSDL";   
        String soapaction = "http://tempuri.org/getBirthday";
        String methodname = "getBirthday";
        SoapObject request = new SoapObject(namespace, methodname);    
        request.addProperty("name","" + txtname.getText());
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE (url);

       
            androidHttpTransport.call(soapaction, envelope);
            org.ksoap2.serialization.SoapObject resultsRequestSOAP =(org.ksoap2.serialization.SoapObject) envelope.bodyIn;
            txtBirthday.setText("" + resultsRequestSOAP.getProperty(0));
        }
        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/txtBirthday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/blank"
        android:textAppearance="?android:attr/textAppearanceLarge" />




    <EditText
        android:id="@+id/txtName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

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

</LinearLayout>

strings.xml

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

    <string name="hello">Hypatia Friend Finder</string>
    <string name="app_name">HypatiaFriends</string>
    <string name="buttontext">Get Birthday</string>
    <string name="blank"></string>

</resources>



AndroidManifest.xml

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

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

</manifest>


The Internet permission is important here, because this application accesses the internet.
<uses-permission
        android:name="android.permission.INTERNET" />

 

1 comment:

  1. Hello,
    How to pass parameters type of object? For example:





























    in there,
    how to pass param to "parameters" node. I didnt do this. I created class name is QueryStringParameters. And mapping with envelop mapping. But property sent null. I didnt. Can you help me?
    (PS: Sorry for my English)

    ReplyDelete