The soap protocol in android calls webservice using of ksoap

  • 2020-05-27 07:05:32
  • OfStack

As shown in the following code:

SoapObject request  = new SoapObject(serviceNamespace, methodName); 

The two parameters of the SoapObject constructor are:
serviceNamespace with your webservice namespace, either can be
http: / / localhost: 8088 / flickrBuddy/services/Buddycast such, also can be urn: PI/DevCentral/SoapService such;
methodName contains the name of the method you want to call.
Then, it is called in the order of the webservice method parameters

request.addProperty( "username", "user" ); 
request.addProperty( "password", "pass" ); 

To populate the webservice parameter.


It is recommended that webservice methods pass arguments of type string whenever possible. Even with the int type, it is possible for kSOAP2 to interact with webservice written by Java.
In the case where the webservice method returns the String type, it is not necessary for the developer to do the serialization (Serialization) customization.

Key points:
kSOAP 1.X /2.0 automatically maps four SOAP types to Java types

SOAP                type Java type 
xsd:int               java.lang.Integer 
xsd:long            java.lang.Long 
xsd:string          java.lang.String 
xsd:boolean     java.lang.Boolean 

In addition, you need to do your own type mapping.
Then tell SoapSerializationEnvelope to wrap the SoapObject in:

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

Key points:

You can specify which specification of SoapSerializationEnvelope or SoapEnvelope you want to use by using the constructor of SoapSerializationEnvelope or SoapEnvelope. It can be one of the following:
Constant SoapEnvelope.VER10: corresponds to the SOAP 1.0 specification
The constant SoapEnvelope.VER11: corresponds to the SOAP 1.1 specification
The constant SoapEnvelope.VER12: corresponds to the SOAP 1.2 specification
This way, no matter which webservice specification is used by the webservice to be invoked, you can easily deal with it.
And then I'm gonna declare

HttpTransport tx = new HttpTransport(serviceURL); 
ht.debug = true; 

The parameters of the HttpTransport constructor are:
The target address to post SOAP data, for example
http: / / soap. amazon. com/onca/soap3.
HttpTransport is a powerful helper class to do Http-call transport process, which encapsulates the one-slice of a network request without you having to worry about serializing messages. We turn on the debug information by setting its debug property to true.
The method () itself can send a request to the server, receive a response from the server, and serialize an SOAP message, as shown below:, envelope); 

The two parameters of HttpTransport's call method are:
The soapAction and SOAP specifications define a new HTTP header called SOAPAction, which must be included in all SOAP HTTP requests (even if they are empty). The SOAPAction header is intended to indicate the intent of the message. You can usually set this parameter to null, so HttpTransport will set HTTP header SOAPAction to be an empty string.
Envelope is the SoapSerializationEnvelope or SoapEnvelope object we constructed earlier.

For the processing of HttpTransport, kSOAP2 and kSOAP1.2 are not written in the same way.
For kSOAP 1.2, the constructor for HttpTransport is HttpTransport (String url, String soapAction), and the second parameter, soapAction, can be the name of the webservice method to call.
For kSOAP 2, the constructor is HttpTransport(String url). kSOAP2 is equivalent to separating out the webservice method name and leaving it entirely to SoapObject to encapsulate, while HttpTransport is only responsible for sending out SoapEnvelope and receiving the response, which is more reasonable.
Calling the call method is a synchronization process that needs to wait for its return.

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

Object Response = envelope.getResult(); 

If the debug property of HttpTransport is true, you can pass at this point

System.out.println("Response dump>>" + tx.responseDump); 

Print out the debug information for HttpTransport. This debugging information is especially useful when exceptions occur to the front call and getResult methods.
Since our webservice method returns string, it is very easy to get this string value:

String sResponse = (String)Response; 

Since the HttpTransport class actually calls HttpConnection for a network connection, you must create another thread to do kSOAP work, otherwise you will block the operation.

Related articles: