As shown in the following code:
Copy Code code as follows:
Soapobject request = new Soapobject (Servicenamespace, MethodName);
The two parameters of the Soapobject constructor mean:
servicenamespace– your WebService namespace, can be either
Http://localhost:8088/flickrBuddy/services/Buddycast such, can also be urn:pi/devcentral/soapservice such;
methodname– the name of the method you want to call.
Then, according to the order of the WebService method parameters, call the
Copy Code code as follows:
Request.addproperty ("username", "user");
Request.addproperty ("Password", "pass");
To populate the WebService parameter.
Attention:
It is recommended that the WebService method pass the parameters as far as possible with the string type. Even with the int type, kSOAP2 and Java-written webservice may also have an interaction exception.
For the WebService method to return a string type, there is no need for developers to do serialization (serialization) customization work.
Points:
Ksoap 1.x/2.0 can automatically map four types of soap to Java types
Copy Code code as follows:
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, developers are required to do the type mapping themselves.
Then tell Soapserializationenvelope to encapsulate the constructed Soapobject:
Copy Code code as follows:
Soapserializationenvelope envelope =
New Soapserializationenvelope (SOAPENVELOPE.VER11);
Envelope.bodyout = Request;
Points:
You can specify which specification of soap you want to use by Soapserializationenvelope or SoapEnvelope constructors, which can be one of the following:
Constant SOAPENVELOPE.VER10: Corresponds to the SOAP 1.0 specification
Constant SOAPENVELOPE.VER11: Corresponds to the SOAP 1.1 specification
Constant Soapenvelope.ver12: Corresponds to the SOAP 1.2 specification
In this way, you can easily handle whatever SOAP specification is used for the webservice you are calling.
Next, we need to declare
Copy Code code as follows:
HttpTransport tx = new HttpTransport (serviceurl);
Ht.debug = true;
The parameter meaning of the HTTPTransport constructor is:
serviceurl– the destination address of the SOAP data to be delivered, for example
Http://soap.amazon.com/onca/soap3.
HTTPTransport is a powerful helper class that completes the Http-call transport process, encapsulates everything that the network requests, and you don't have to consider serialization messages at all. We turn on debug information by setting its Debug property to True.
Method Httptransport.call () itself is able to send a request to the server, receive a response from the server, and serialize the SOAP message as follows:
Copy Code code as follows:
Ht.call (null, envelope);
The HTTPTransport call method has two parameters that mean:
The SOAPACTION–SOAP specification defines a new HTTP header named SOAPAction, and all SOAP HTTP requests, even if they are empty, must contain the header. The SOAPAction header is intended to indicate the intent of the message. You can usually set this parameter to NULL so that HTTPTransport sets the HTTP header soapaction to be an empty string.
envelope– is the Soapserializationenvelope or SoapEnvelope object that we constructed earlier.
Attention:
For httptransport processing, kSOAP2 and kSOAP1.2 are not the same.
The constructor for the Ksoap 1.2,httptransport is httptransport (string url, String soapaction), and the second argument SOAPAction can be the name of the WebService method to invoke.
and Ksoap 2, the constructor is HttpTransport (String URL). KSOAP2 is equivalent to separate the WebService method name, completely to Soapobject to encapsulate, and httptransport only responsible for the SoapEnvelope sent out and receive responses, so more reasonable.
Calling the call method is a synchronization process that needs to wait for it to return.
Once returned, you can call the Soapserializationenvelope GetResult method to get the result:
Copy Code code as follows:
Object Response = Envelope.getresult ();
If the Debug property of HTTPTransport is true, you can now pass the
Copy Code code as follows:
System.out.println ("Response dump>>" + tx.responsedump);
Print out debugging information for HTTPTransport. This debugging information is especially useful when the previous call method and the GetResult method have an exception.
Before our WebService method is to return a string, the string value is very simple:
Copy Code code as follows:
String sresponse = (string) Response;
Attention:
Because the HTTPTransport class is actually calling httpconnection for a network connection, it is necessary to have a separate thread to do ksoap work, otherwise blocking the operation.