Thursday, February 5, 2015
Android Web service Access Tutorial
Updated tutorial for new versions >> http://codeoncloud.blogspot.com/2013/06/android-java-soap-web-service-access.html
This simple example demonstrates how we can access a java web service from Android application.
We cant connect Android 3.0 and after versions to the internet in the main thread. So we have to start new thread. In this tutorial Im going to demonstrate how we can access a simple java web service by starting new thread. I have tested this on Android 4.0.3 platform.
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 posts.
1. Create java web service in Eclipse using Axis2 (Part 01)
2. Create java web service in Eclipse using Axis2 (Part 02)
Note:
URL in line 16 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. I think it is easy to find those things if you open WSDL from a web browser. For more details look the image :

Add Internet permission to Androidanifest.xml file.(Look highlighted line)This simple example demonstrates how we can access a java web service from Android application.
We cant connect Android 3.0 and after versions to the internet in the main thread. So we have to start new thread. In this tutorial Im going to demonstrate how we can access a simple java web service by starting new thread. I have tested this on Android 4.0.3 platform.
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 posts.
1. Create java web service in Eclipse using Axis2 (Part 01)
2. Create java web service in Eclipse using Axis2 (Part 02)
package com.android.ws;Here is the code for Android application to invoke deployed web service.
public class PrintMsg {
public String sayHello(){
return "Hello Chathura";
}
}
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();
}
}
}
Note:
SOAP_ACTION in line 13 is "NAMESPACE/METHOD_NAME".
METHOD_NAME in line 14 is WSDL operation. You can find something like <wsdl:operation name="sayHello"> in your WSDL sayHello is METHOD_NAME here.
METHOD_NAME in line 14 is WSDL operation. You can find something like <wsdl:operation name="sayHello"> in your WSDL sayHello is METHOD_NAME here.
NAMESPACE in line 15 is targetNamespace in the WSDL. Replace that & add a "/" to the end .
/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. I think it is easy to find those things if you open WSDL from a web browser. For more details look the image :

}
Run the application using Emulator.
Result :

You can download updated project here here
Password :cloud
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.