Constant | Description |
---|---|
NAMESPACE | Namespace is the targetNamespace in the WSDL. |
URL | The WSDL URL. Its value is the location attribute of the soap:address element for a port element in a WSDL. Unless the web service is also hosted on the Android device, the hostname should not be specified as localhost , because the application runs on the Android device while the web service is hosted on the localhost server. Specify hostname as the IP address of the server hosting the web service. |
METHOD_NAME | The name of the web service operation, which may be obtained form the WSDL. |
SOAP_ACTION | NAMESPACE+METHOD_NAME specified as a String literal. |
The constant values are as follows; the IP address for the URL constant would vary:
private static final String NAMESPACE = "http://hello_webservice/"; private static String URL="http://192.168.1.68:7001/HelloWebService/HelloWSService?WSDL"; private static final String METHOD_NAME = "hello"; private static final String SOAP_ACTION = "http://hello_webservice/hello"; |
All Activities must implement the
onCreate
method for activity initialization. Define the UI using the setContentView
method and the layout resource.setContentView(R.layout.main); |
Create an Android widget
TextView
object using the findViewById
method on the TextView
element defined in the main.xml
.TextView lblResult = (TextView) findViewById(R.id.result); |
Create a
org.ksoap2.serialization.SoapObject
object to build a SOAP request. Specify the namespace of the SOAP object and method name to be invoked in the SoapObject
constructor.SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); |
Create a
org.ksoap2.serialization.PropertyInfo
object to contain property information to be sent with the SOAP method call. Each property requires a new PropertyInfo
object. The hello
method takes only 1 argument for a name. Set the property name as "arg0", and specify the type of the property as STRING_CLASS
. Add the PropertyInfo
object to the SoapObject
using the addProperty
method.PropertyInfo propInfo=new PropertyInfo(); propInfo.name="arg0"; propInfo.type=PropertyInfo.STRING_CLASS; request.addProperty(propInfo, "John Smith"); |
Next create a SOAP envelop. Use the
SoapSerializationEnvelope
class, which extends the SoapEnvelop
class, with support for SOAP Serialization format, which represents the structure of a SOAP serialized message. The main advantage of SOAP serialization is portability.SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); |
The constant
SoapEnvelope.VER11
indicates SOAP Version 1.1. Assign the SoapObject
request object to the envelop as the outbound message for the SOAP method call.envelope.setOutputSoapObject(request); |
Create a
org.ksoap2.transport.HttpTransportSE
object that represents a J2SE based HttpTransport
layer.HttpTransportSE
extends the org.ksoap2.transport.Transport
class, which encapsulates the serialization and deserialization of SOAP messages.HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); |
Make the soap call using the
SOAP_ACTION
and the soap envelop.androidHttpTransport.call(SOAP_ACTION, envelope); |
Get the web service response using the
getResponse
method of the SoapSerializationEnvelope
object and cast the response object to SoapPrimitive
, class used to encapsulate primitive types.SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse(); |
Set the String message in the SOAP response in the
TextView
UI component.lblResult.setText(resultsRequestSOAP.toString()); |
The
Activity
class is listed below in Listing 4.Listing 4. Activity Class AndroidWSClient.java
package android.webservice.client; 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 org.ksoap2.serialization.PropertyInfo; import android.widget.TextView; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class AndroidWSClient extends Activity { private static final String NAMESPACE = "http://hello_webservice/"; private static String URL = "http://192.168.1.68:7001/HelloWebService/ HelloWSService?WSDL"; private static final String METHOD_NAME = "hello"; private static final String SOAP_ACTION = "http://hello_webservice/hello"; private TextView lblResult; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lblResult = (TextView) findViewById(R.id.result); SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); PropertyInfo propInfo=new PropertyInfo(); propInfo.name="arg0"; propInfo.type=PropertyInfo.STRING_CLASS; request.addProperty(propInfo, "John Smith"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try { androidHttpTransport.call(SOAP_ACTION, envelope); SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse(); lblResult.setText(resultsRequestSOAP.toString()); } catch (Exception e) { } } } |
No comments:
Post a Comment