SoFunction
Updated on 2025-04-07

Using soap protocol in Android (ksoap calls webservice)

As shown in the following code:

Copy the codeThe code is as follows:

SoapObject request  = new SoapObject(serviceNamespace, methodName);

The two parameters of the SoapObject constructor are:
serviceNamespace – The namespace of your webservice, either
http://localhost:8088/flickrBuddy/services/Buddycast, or urn:PI/DevCentral/SoapService;
methodName – The name of the method you want to call.
Then, in the order of the webservice method parameters, call it in turn

Copy the codeThe code is as follows:

( "username", "user" );
( "password", "pass" );

to populate the webservice parameters.

Notice:

It is recommended that the parameters passed by the webservice method should be string type as much as possible. Even for int type, kSOAP2 may interact with webservice written in Java.
When the webservice method returns String type, there is no need for the developer to do serialization customization.

Key points:
kSOAP /2.0 can automatically map four SOAP types to Java types

Copy the codeThe code is as follows:

SOAP                type Java type
xsd:int              
xsd:long           
xsd:string         
xsd:boolean    

In addition, developers need to do their own type mapping.
Then tell SoapSerializationEnvelope to encapsulate the constructed SoapObject:

Copy the codeThe code is as follows:

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

Key points:

You can use the SoapSerializationEnvelope or SoapEnvelope constructor to indicate which specification you want to use. It can be one of the following:
Constant SoapEnvelope.VER10: Corresponding to SOAP 1.0 specification
Constant SoapEnvelope.VER11: Corresponding to SOAP 1.1 specification
Constant SoapEnvelope.VER12: Corresponding to SOAP 1.2 specification
In this way, you can easily deal with it no matter which SOAP specification the webservice is called.
Next, we will declare

Copy the codeThe code is as follows:

HttpTransport tx = new HttpTransport(serviceURL);
= true;

The parameters of the HttpTransport constructor are:
serviceURL – The destination address to deliver SOAP data, for example
/onca/soap3 。
HttpTransport is a powerful auxiliary class to complete the Http-call transport process. It encapsulates everything requested by the network, and you don't have to consider serializing messages at all. We turn on debug information by setting its debug property to true.
Method () can send a request to the server, receive the server response and serialize the SOAP message, as shown below:

Copy the codeThe code is as follows:

(null, envelope);

The two parameters of the call method of HttpTransport are:
soapAction – The SOAP specification defines a new HTTP header named SOAPAction that must include all SOAP HTTP requests (even if empty). The SOAPAction header is intended to indicate the intent of the message. This parameter can usually be set to null, so that HttpTransport will set the HTTP header SOAPAction to an empty string.
Envelope – is the SoapSerializationEnvelope or SoapEnvelope object we constructed earlier.

Notice:
In terms of HttpTransport processing, kSOAP2 and kSOAP1.2 are written differently.
For kSOAP 1.2, the constructor of HttpTransport is HttpTransport (String url, String soapAction), and the second parameter soapAction can be the name of the webservice method to be called.
For kSOAP 2, the constructor is HttpTransport(String url). kSOAP2 is equivalent to separating the webservice method name and giving it to SoapObject for encapsulation. HttpTransport is only responsible for sending SoapEnvelope and receiving responses, which is more reasonable.
Calling the call method is a synchronization process and needs to wait for it to return.

After returning, you can call the getResult method of SoapSerializationEnvelope to get the result:

Copy the codeThe code is as follows:

Object Response = ();

If the debug property of HttpTransport is true, then you can pass

Copy the codeThe code is as follows:

("Response dump>>" + );

Print out the debugging information of HttpTransport. Especially when the call method and getResult method have an exception, this debugging information is very useful.
Since our webservice method returns string, it is very simple to get this string value:

Copy the codeThe code is as follows:

String sResponse = (String)Response;

Notice:
Since the HttpTransport class actually calls HttpConnection for network connection, another thread must be set up to do kSOAP work specifically, otherwise the operation will be blocked.