国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 中利用 ksoap2 調(diào)用 WebService的示例代碼

2019-12-12 01:58:49
字體:
供稿:網(wǎng)友

去年剛剛工作的時候,第一個項目是要訪問 WebService。由于沒有接觸過,查了很多資料,在別人代碼的基礎(chǔ)上稍微修改了一下,總算滿足了自己的需求。最近整理電腦的時候,發(fā)現(xiàn)了這個 WebService 的訪問類,怕哪一天需要的時候找不到了,干脆寫在博客上吧,也給需要的人提供一個參考。

1.下載 ksoap2 的 jar 文件

下載地址:ksoap2-android-assembly-3.6.1-jar-with-dependencies.jar

下載完成后依賴到自己的項目中即可。

2.封裝網(wǎng)絡(luò)訪問工具類

直接貼代碼了,注釋寫的很詳細(xì),根據(jù)自己的需要加以修改。

/** * 訪問 WebService 的工具類 */public class WebServiceUtil { // 命名空間 private static final String NAMESPACE = "your namespace"; // WebService 服務(wù)器地址 private static final String ENDPOINT = "your address"; // 一般自己公司開發(fā)都是需要身份驗證的 // 身份驗證方法名 private static final String ID_HEADERNAME = "verify method"; // 身份驗證 key private static final String ID_NAME_PARAM = "verify key1"; // 身份驗證 value private static final String ID_NAME_VALUE = "verify value1"; // 身份驗證 key private static final String ID_PASSWORD_PARAM = "verify key2"; // 身份驗證 value private static final String ID_PASSWORD_VALUE = "verify value2"; // 訪問的服務(wù)器是否由 dotNet 開發(fā) public static boolean isDotNet = true; // 線程池的大小 private static int threadSize = 5; // 創(chuàng)建一個可重用固定線程數(shù)的線程池,以共享的無界隊列方式來運行這些線程 private static ExecutorService threadPool = Executors.newFixedThreadPool(threadSize); // 連接響應(yīng)標(biāo)示 public static final int SUCCESS_FLAG = 0; public static final int ERROR_FLAG = 1; /**  * 調(diào)用 WebService 接口  *  * @param methodName  WebService 的調(diào)用方法名  * @param mapParams  WebService 的參數(shù)集合,可以為 null  * @param reponseCallBack 服務(wù)器響應(yīng)接口  */ public static void call(final String methodName, SimpleArrayMap<String, Object> mapParams, final ResponseCallBack reponseCallBack) {  // 創(chuàng)建 HttpTransportSE 對象,傳遞 WebService 服務(wù)器地址  final HttpTransportSE transport = new HttpTransportSE(ENDPOINT);  transport.debug = true;  // 身份驗證(如果需要的話)  Element[] header = new Element[1];  // 傳入命名空間與驗證的方法名  header[0] = new Element().createElement(NAMESPACE, ID_HEADERNAME);  // 創(chuàng)建參數(shù) 1  Element userName = new Element().createElement(NAMESPACE, ID_NAME_PARAM);  userName.addChild(Node.TEXT, ID_NAME_VALUE);  header[0].addChild(Node.ELEMENT, userName);  // 創(chuàng)建參數(shù) 2  Element password = new Element().createElement(NAMESPACE, ID_PASSWORD_PARAM);  password.addChild(Node.TEXT, ID_PASSWORD_VALUE);  header[0].addChild(Node.ELEMENT, password);  // 創(chuàng)建 SoapObject 對象用于傳遞請求參數(shù)  final SoapObject soapObject = new SoapObject(NAMESPACE, methodName);  // 添加參數(shù)  if (mapParams != null) {   for (int index = 0; index < mapParams.size(); index++) {    String key = mapParams.keyAt(index);    // 多數(shù)情況下,傳遞的參數(shù)都為 String 類型,不過少數(shù)情況下會有 boolean 類型,所以用 Object 代替    Object value = mapParams.get(key);    soapObject.addProperty(key, value);   }  }  // 實例化 SoapSerializationEnvelope,傳入 WebService 的 SOAP 協(xié)議的版本號  // 這里有 VER10 VER11 VER12 三種版本,根據(jù)自己需要填寫  final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  envelope.headerOut = header; // 身份驗證(如果需要的話)  envelope.dotNet = isDotNet; // 設(shè)置是否調(diào)用的是 .Net 開發(fā)的 WebService  envelope.bodyOut = soapObject; // 傳遞參數(shù)  //envelope.setOutputSoapObject(soapObject);// 與上一句等價  // 用于與主線程通信的 Handler  final Handler responseHandler = new Handler() {   @Override   public void handleMessage(Message msg) {    super.handleMessage(msg);    // 根據(jù)消息的 arg1 值判斷調(diào)用哪個接口    if (msg.arg1 == SUCCESS_FLAG) {     reponseCallBack.onSuccess((String) msg.obj);    } else {     reponseCallBack.onError((Exception) msg.obj);    }   }  };  // 提交一個子線程到線程池并在此線種內(nèi)調(diào)用 WebService  if (threadPool == null || threadPool.isShutdown()) {   threadPool = Executors.newFixedThreadPool(threadSize);  }  threadPool.submit(new Runnable() {   @Override   public void run() {    String result = null;    try {     // 解決 EOFException     System.setProperty("http.keepAlive", "false");     // 連接服務(wù)器,有的服務(wù)可能不需要傳遞 NAMESPACE + methodName,第一個參數(shù)傳遞 null     transport.call(null, envelope);     if (envelope.getResponse() != null) {      // 獲取服務(wù)器響應(yīng)返回的 SoapObject      SoapObject object = (SoapObject) envelope.bodyIn;      result = object.getProperty(0).toString();     }    } catch (IOException e) {     // 當(dāng) call 方法的第一個參數(shù)為 null 時會有一定的概念拋 IO 異常     // 因此需要需要捕捉此異常后用命名空間加方法名作為參數(shù)重新連接     // e.printStackTrace();     try {      transport.call(NAMESPACE + methodName, envelope);      if (envelope.getResponse() != null) {       // 獲取服務(wù)器響應(yīng)返回的 SoapObject       SoapObject object = (SoapObject) envelope.bodyIn;       result = object.getProperty(0).toString();      }     } catch (Exception e1) {      // e1.printStackTrace();      responseHandler.sendMessage(responseHandler.obtainMessage(0, ERROR_FLAG, 0, e1));     }    } catch (XmlPullParserException e) {     // e.printStackTrace();     responseHandler.sendMessage(responseHandler.obtainMessage(0, ERROR_FLAG, 0, e));    } finally {     // 將獲取的消息利用 Handler 發(fā)送到主線程     responseHandler.sendMessage(responseHandler.obtainMessage(0, SUCCESS_FLAG, 0, result));    }   }  }); } /**  * 設(shè)置線程池的大小  *  * @param threadSize  */ public static void setThreadSize(int threadSize) {  WebServiceUtil.threadSize = threadSize;  threadPool.shutdownNow();  threadPool = Executors.newFixedThreadPool(WebServiceUtil.threadSize); } /**  * 服務(wù)器響應(yīng)接口,在響應(yīng)后需要回調(diào)此接口  */ public interface ResponseCallBack {  void onSuccess(String result);  void onError(Exception e); }}

3.在 Activity 中使用

private void request() {  SimpleArrayMap<String, Object> map = new SimpleArrayMap<>();  map.put("key1", "value1");  map.put("key2", "value2");  WebServiceUtil.call("method name", map, new WebServiceUtil.ResponseCallBack() {   @Override   public void onSuccess(String result) {    // 請求成功   }   @Override   public void onError(Exception e) {    // 請求失敗   }  }); }

至此調(diào)用結(jié)束,有需要訪問 WebService 的同學(xué)可以參考一下。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿拉善左旗| 灵宝市| 德兴市| 长兴县| 金寨县| 汾西县| 宁都县| 乳山市| 久治县| 当阳市| 海南省| 红河县| 芷江| 太保市| 瑞丽市| 永兴县| 句容市| 云和县| 山阳县| 罗田县| 万荣县| 驻马店市| 和龙市| 连南| 辽源市| 怀远县| 祁阳县| 定安县| 长葛市| 博爱县| 大丰市| 红桥区| 清新县| 新昌县| 会宁县| 句容市| 湖州市| 颍上县| 随州市| 陆丰市| 沙河市|