Saturday, January 17, 2015
Android Web Service Access Tutorial
In this tutorial Im going to demonstrate how we can access a java web service in Android application using ksoap2 library. This Android application also passes parameters to the web service.
First we have to create a web service & deploy it on Tomcat server.
Here is the sample code of java web service Im going to access.
package com.testprops.ws;
public class TestPropts {
public String testMyProps(String fname,String lname){
return "First Name : "+fname+" Last Name : "+lname;
}
}
Android application will pass parameters for fname & lname variables.
To create web service refer my these tutorials.
1. Create java web service in Eclipse using Axis2 (Part 01)
2. Create java web service in Eclipse using Axis2 (Part 02)
Following is the code for the Android application which invoke the web service. You have to use ksoap2 (You can download ksoap2 from here:::http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2) library for this implementation. Read my comments carefully
package com.sendproperties.ws;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SendValuesActivity extends Activity {
private final String NAMESPACE = "http://ws.testprops.com";
private final String URL = "http://175.157.143.117:8085/TestProperties/services/TestPropts?wsdl";
private final String SOAP_ACTION = "http://ws.testprops.com/testMyProps";
private final String METHOD_NAME = "testMyProps";
/** 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);
String firstName = "Android";
String lastName = "Program";
//Pass value for fname variable of the web service
PropertyInfo fnameProp =new PropertyInfo();
fnameProp.setName("fname");//Define the variable name in the web service method
fnameProp.setValue(firstName);//Define value for fname variable
fnameProp.setType(String.class);//Define the type of the variable
request.addProperty(fnameProp);//Pass properties to the variable
//Pass value for lname variable of the web service
PropertyInfo lnameProp =new PropertyInfo();
lnameProp.setName("lname");
lnameProp.setValue(lastName);
lnameProp.setType(String.class);
request.addProperty(lnameProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
TextView tv = new TextView(this);
tv.setText(response.toString());
setContentView(tv);
} catch (Exception e) {
e.printStackTrace();
}
}
}
You can find complete information about the implementation of Android application from these two posts.
1. Create java web service in Eclipse using Axis2 (Part 01)
2. Create java web service in Eclipse using Axis2 (Part 02)
Change these things in your Android program according to your web service
NAMESPACE in line 15 is targetNamespace in the WSDL.
blue colored is the ip of the server replace it with your ip & red colored is the port number.
SOAP_ACTION in line 17 is "NAMESPACE/METHOD_NAME"
METHOD_NAME in line 18 is WSDL operation. You can find something like <wsdl:operation name="............."> in your WSDL.
METHOD_NAME in line 18 is WSDL operation. You can find something like <wsdl:operation name="............."> in your WSDL.
Make appropriate changes according to your WSDL. Open your WSDL using Firefox or Chrome. Then you can easily find those values from the WSDL.
We have to add internet permission to the Android Manifest. After adding permission your Manifest should like follows.
Here is the final result
You can download sample projects:
Click here to download web service project
Click here to download Android project
If you find this post helpful dont forget to leave a comment. your comments encourage me to write!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.