728x90 AdSpace

Saturday, February 11, 2012

Android Web Service Access Using ksoap2. - Tutorials Views

Android Web Service Access Using ksoap2.

In this tutorial I'm going to demonstrate how we can access a simple java web service  using an Android application. To complete this tutorial you need some knowledge about dynamic web service projects in Eclipse & about ksoap2.

Following is the sample java code for web service class. Deploy this web service on Tomcat server at local host. To implement this web service follow these two posts Post 1 || Post 2
package com.android.ws;
public class PrintMsg {
 public String sayHello(){
           return "Hello Chathura";
        }
}

Following is the code which we can use in Android application to invoke deployed web service.
package com.androidclient.ws;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.widget.TextView;

import android.app.Activity;
import android.os.Bundle;

public class WSClientActivity extends Activity {
    private static final String SOAP_ACTION = "http://ws.android.com/sayHello";
    private static final String METHOD_NAME = "sayHello";
    private static final String NAMESPACE = "http://ws.android.com/";
    private static final String URL = "http://175.157.229.119:8080/AndroidWSTest/services/PrintMsg?wsdl";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);           

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        
        envelope.setOutputSoapObject(request);

        HttpTransportSE ht = new HttpTransportSE(URL);
        try {
         ht.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            
            TextView tv = new TextView(this);
            tv.setText("Message :"+response.toString());
            setContentView(tv);
  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}

Here:
SOAP_ACTION in line 14 is "NAMESPACE/METHOD_NAME"

METHOD_NAME
in line 15 is WSDL operation. You can find something like <wsdl:operation name="sayHello"> in your WSDL sayHello is METHOD_NAME here.

NAMESPACE in line 16 is targetNamespace in the WSDL. Replace that & add a "/" to the end .

URL in line 17 The URL of WSDL file. In my case it is http://175.157.229.119:8080
/AndroidWSTest/services/PrintMsg?wsdl
blue colored is the ip of the server replace it with your ip & red colored is the port number

Make appropriate changes  according to your WSDL. Following image will help you.

This is an example image of a WSDL opened using Firefox .


Add Internet permission to Androidanifest.xml file.



    

    
        
            
                

                
            
        
    
Highlighted line shows the necessary changes.
Run the application using Emulator.Result :


Note :
For Android 3.0 and after versions we cannot connect to the internet in the main thread. So we have to start new thread.

Here is the code :

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;

public class AndroidWSClientActivity extends Activity {
 
    private static final String SOAP_ACTION = "http://ws.android.com/sayHello";
    private static final String METHOD_NAME = "sayHello";
    private static final String NAMESPACE = "http://ws.android.com/";
    private static final String URL = "http://175.157.229.119:8080/AndroidWSTest/services/PrintMsg?wsdl";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     
    Thread networkThread = new Thread() {
    @Override
    public void run() {
      try {
         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);          
         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.setOutputSoapObject(request);
         
         HttpTransportSE ht = new HttpTransportSE(URL);
         ht.call(SOAP_ACTION, envelope);
         final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
         final String str = response.toString();

         runOnUiThread (new Runnable(){ 
     public void run() {
         TextView result;
         result = (TextView)findViewById(R.id.textView1);//Text view id is textView1
         result.setText(str);
           }
       });
      } 
     catch (Exception e) {
         e.printStackTrace();
     }
    }
  };
  networkThread.start();
  }
 }
}

You can download updated project (for new versions) here
Password : cloud

If you find this post helpful don't forget to leave a comment. Your comments always encourage me to write more!
  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Item Reviewed: Android Web Service Access Using ksoap2. - Tutorials Views Rating: 5 Reviewed By: Hasan Afaque
Scroll to Top