SOAP:簡單對象訪問協議,簡單對象訪問協議(SOAP)是一種輕量的、簡單的、基於 XML 的協議。 php
經過第三方提供的架包ksoap2-Android-assembly-2.4-jar-with-dependencies.jar,咱們能夠向服務器進行請求調用本身須要的服務。下面以http://www.webxml.com.cn/提供的天氣預報web服務爲例。 java
下面是向遠處服務器進行請求的詳細操做類WebServiceUtil android
- 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協議的訪問操做。 web
在訪問遠程服務器提供的服務時,有時會由於網絡問題或者是服務器端問題,而致使客戶端側一直處於請求鏈接狀態,此時咱們但願能夠控制請求得不到響應的超時時間TimeOut. 服務器
想要控制請求的超時時間,咱們須要根據ksoap2-android-assembly-2.4-jar-with-dependencies.jar包,修改一些訪問的控制類。 網絡
1.首先重寫架包中的ServiceConnectionSE.Java,添加設置超時時間的方法,能夠在你的工程裏重寫這個類 this
複製到剪貼板 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 url
複製到剪貼板 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;