您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么在Android中利用 ksoap2對WebService進(jìn)行調(diào)用 ,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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) { // 請求失敗 } }); }
以上就是怎么在Android中利用 ksoap2對WebService進(jìn)行調(diào)用 ,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。