Use KSOAP2


Import ksoap into an Android project in Eclipse.

ksoap is a SOAP API suitable for the J2ME, based on kXML. We can use ksoap in Android to access soap based web services easily. In this tutorial I’m not going to develop any web service. This will describe how to add ksoap in to your Android project when you are using Eclipse IDE.

Pre-requisites
1. Eclipse with ADT.
2. Android SDK.
3. ksoap (Click here to download ksoap2 jar file).

Adding ksoap is very easy. In the same way we can add any other library file.
Follow this steps to add ksoap2(or any other library file) in to existing Android project.

1. First download & copy ksoap2.jar in to documents or any other accessible directory.
2. Right click on your Android project.
3. Select properties.
4. In properties window select Java Build Path > Libraries > Add External JARs.
5. Select ksoap2.jar file. Then click Open & finally click OK.

 

 

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

1
2
3
4
5
6
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<manifest android:versioncode="1" android:versionname="1.0" package="com.androidclient.ws" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minsdkversion="8">
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:label="@string/app_name" android:name=".MyFirstAndroidActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN">
                <category android:name="android.intent.category.LAUNCHER">
            </category></action></intent-filter>
        </activity>
    </application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</uses-sdk></manifest>

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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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();
  }
 }
}

 

OK thanks

Nguon  : http://javatutorialspoint.blogspot.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: