SOAP:簡易物件存取通訊協定 (SOAP),簡易物件存取通訊協定 (SOAP)(SOAP)是一種輕量的、簡單的、基於 XML 的協議。
通過第三方提供的架包ksoap2-Android-assembly-2.4-jar-with-dependencies.jar,我們可以向伺服器進行請求調用自己需要的服務。下面以http://www.webxml.com.cn/提供的天氣預報web服務為例。
下面是向遠處伺服器進行請求的詳細操作類WebServiceUtil
複製到剪貼簿 Java代碼
public class WebServiceUtil {
//命名空間
private static final String NAMESPACE = "http://WebXml.com.cn/";
//WebService地址
private static final String URL = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
//需要調用的方法名
private static final String getSupportProvince = "getSupportProvince";
/**
* @desc 獲得洲、國內外省份和城市資訊
* @return 省份列表
*/
public List getAllProvince() {
List allProvince = new ArrayList();
try {
//1.執行個體化SoapObject對象
SoapObject request = new SoapObject(NAMESPACE, getSupportProvince);
//2.如果方法需要參數,設定參數
// request.setProperty("參數名稱", "參數值");
//3.設定Soap的請求資訊,參數部分為Soap協議的版本號碼
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
//4.構建傳輸對象
AndroidHttpTransport transport = new AndroidHttpTransport(URL);
transport.debug = true;
//5.訪問WebService,第一個參數為命名空間 + 方法名,第二個參數為Envelope對象
transport.call(NAMESPACE + getSupportProvince, envelope);
//6.解析返回的資料
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;
}
}
使用還是比較簡單的,在這我只以天氣預報服務中提供的擷取省份資訊的方法getSupportProvince為例,詳細的解釋了基於soap協議的訪問操作。
在訪問遠程伺服器提供的服務時,有時會因為網路問題或者是伺服器端問題,而導致用戶端側一直處於請求串連狀態,此時我們希望可以控制請求得不到響應的逾時時間TimeOut.
想要控制請求的逾時時間,我們需要根據ksoap2-android-assembly-2.4-jar-with-dependencies.jar包,修改一些訪問的控制類。
1.首先重寫架包中的ServiceConnectionSE.Java,添加設定逾時時間的方法,可以在你的工程裡重寫這個類
複製到剪貼簿 Java代碼
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();
}
//設定串連伺服器的逾時時間,毫秒級,此為自己添加的方法
public void setConnectionTimeOut(int timeout){
this.connection.setConnectTimeout(timeout);
}
}
再自己寫一個傳輸對象類,類似於架包中的AndroidHttpTransport類,命名為MyAndroidHttpTransport.java
複製到剪貼簿 Java代碼
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; //預設逾時時間為30s
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);
}
}
完成這之後,在前面的第四步構建傳輸對象中,就不要使用架包中的AndroidHttpTransport,而使用我們自己的寫的這個類。
複製到剪貼簿 Java代碼
//4.構建傳輸對象
// AndroidHttpTransport transport = new AndroidHttpTransport(URL);
// transport.debug = true;
int timeout = 15000; //set timeout 15s
MyAndroidHttpTransport transport = new MyAndroidHttpTransport(URL, timeout);
transport.debug = true;