SOAP: Simple Object Access Protocol. Simple Object Access Protocol (SOAP) is a lightweight, simple, XML-based protocol.
Through the third-party package ksoap2-Android-assembly-2.4-jar-with-dependencies.jar, we can request the server to call the services we need. The following uses the weather forecast web service at http://www.webxml.com.cn/?______ as an example.
The following is the detailed operation class WebServiceUtil for requests to distant servers.
Copy the Java code to the clipboard
Public class WebServiceUtil {
// Namespace
Private static final String NAMESPACE = "http://WebXml.com.cn /";
// WebService address
Private static final String URL = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx? Wsdl ";
// Name of the method to be called
Private static final String getSupportProvince = "getSupportProvince ";
/**
* @ Desc obtain information about continents, domestic and foreign provinces, and cities
* @ Return province list
*/
Public List getAllProvince (){
List allProvince = new ArrayList ();
Try {
// 1. instantiate a SoapObject object
SoapObject request = new SoapObject (NAMESPACE, getSupportProvince );
// 2. If the method requires parameters, set the parameters
// Request. setProperty ("parameter name", "parameter value ");
// 3. Set the Soap request information. The parameter part is the Soap Protocol version number.
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER11 );
Envelope. bodyOut = request;
// 4. Build a transmission object
AndroidHttpTransport transport = new AndroidHttpTransport (URL );
Transport. debug = true;
// 5. Access WebService. The first parameter is the namespace + method name, and the second parameter is the Envelope object.
Transport. call (NAMESPACE + getSupportProvince, envelope );
// 6. parse the returned data
SoapObject result = (SoapObject) envelope. getResponse ();
Int count = result. getPropertyCount ();
For (int I = 0; I <count; I ++ ){
AllProvince. add (result. getProperty (I). toString ());
}
} Catch (IOException e ){
E. printStackTrace ();
} Catch (XmlPullParserException e ){
E. printStackTrace ();
}
Return allProvince;
}
}
It is relatively simple to use. Here I only take the getSupportProvince method provided by the weather forecast service as an example to explain in detail the access operations based on the soap protocol.
When accessing the services provided by the remote server, the client side is always in the request connection status due to network problems or server issues, in this case, we want to control the TimeOut time of the request not responding.
To control the request timeout, we need to modify some access control classes based on the ksoap2-android-assembly-2.4-jar-with-dependencies.jar package.
1. First, rewrite ServiceConnectionSE. Java in the rack package and add a method to set the timeout. You can override this class in your project.
Copy the Java code to the clipboard
Package com. ahutzh. weather;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java.net. HttpURLConnection;
Import java.net. URL;
Import org. ksoap2.transport. ServiceConnection;
Public class ServiceConnectionSE
Implements ServiceConnection
{
Private HttpURLConnection connection;
Public ServiceConnectionSE (String url)
Throws IOException
{
This. connection = (HttpURLConnection) new URL (url). openConnection ());
This. connection. setUseCaches (false );
This. connection. setDoOutput (true );
This. connection. setDoInput (true );
}
Public void connect () throws IOException {
This. connection. connect ();
}
Public void disconnect (){
This. connection. disconnect ();
}
Public void setRequestProperty (String string, String soapAction ){
This. connection. setRequestProperty (string, soapAction );
}
Public void setRequestMethod (String requestMethod) throws IOException {
This. connection. setRequestMethod (requestMethod );
}
Public OutputStream openOutputStream () throws IOException {
Return this. connection. getOutputStream ();
}
Public InputStream openInputStream () throws IOException {
Return this. connection. getInputStream ();
}
Public InputStream getErrorStream (){
Return this. connection. getErrorStream ();
}
// Set the timeout time for connecting to the server in milliseconds. This is the method you have added.
Public void setConnectionTimeOut (int timeout ){
This. connection. setConnectTimeout (timeout );
}
}
Write a transmission object class by yourself. It is similar to the AndroidHttpTransport class in the rack package and is named MyAndroidHttpTransport. java.
Copy the Java code to the clipboard
Package com. ahutzh. weather;
Import java. io. IOException;
Import org. ksoap2.transport. HttpTransportSE;
Import org. ksoap2.transport. ServiceConnection;
Public class MyAndroidHttpTransport extends HttpTransportSE {
Private int timeout = 30000; // The default timeout value is 30 s.
Public MyAndroidHttpTransport (String url ){
Super (url );
}
Public MyAndroidHttpTransport (String url, int timeout ){
Super (url );
This. timeout = timeout;
}
Protected ServiceConnection getServiceConnection (String url) throws IOException {
ServiceConnectionSE serviceConnection = new ServiceConnectionSE (url );
ServiceConnection. setConnectionTimeOut (timeout );
Return new ServiceConnectionSE (url );
}
}
After this is done, do not use AndroidHttpTransport in the rack package in the first step of building the transport object, instead use the class we write.
Copy the Java code to the clipboard
// 4. Build a transmission object
// AndroidHttpTransport transport = new AndroidHttpTransport (URL );
// Transport. debug = true;
Int timeout = 15000; // set timeout 15 s
MyAndroidHttpTransport transport = new MyAndroidHttpTransport (URL, timeout );
Transport. debug = true;