博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ToolsSoap 访问webservice的工具类
阅读量:5766 次
发布时间:2019-06-18

本文共 3460 字,大约阅读时间需要 11 分钟。

package com.zftlive.android.tools;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import android.os.Handler;import android.os.Message;/** * 访问WebService的工具类, *  * @version 1.0 *  */public class ToolSOAP {        // 含有3个线程的线程�?    private static final ExecutorService executorService = Executors.newFixedThreadPool(3);    /**     *      * @param url WebService服务器地址     * @param namespace 命名空间     * @param methodName WebService的调用方法名     * @param properties WebService的参�?     * @param webServiceCallBack 返回结果回调接口     */    public static void callService(String url,final String namespace,final String methodName,HashMap
properties,final WebServiceCallBack webServiceCallBack) { // 创建HttpTransportSE对象,传递WebService服务器地址 final HttpTransportSE httpTransportSE = new HttpTransportSE(url); // 创建SoapObject对象 SoapObject soapObject = new SoapObject(namespace, methodName); // SoapObject添加参数 if (properties != null) { for (Iterator
> it = properties.entrySet().iterator(); it.hasNext();) { Map.Entry
entry = it.next(); soapObject.addProperty(entry.getKey(), entry.getValue()); } } // 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号 final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // 设置是否调用的是.Net开发的WebService soapEnvelope.setOutputSoapObject(soapObject); soapEnvelope.dotNet = true; httpTransportSE.debug = true; // 用于子线程与主线程通信的Handler final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what == 0){ webServiceCallBack.onSucced((SoapObject) msg.obj); }else{ webServiceCallBack.onFailure((String)msg.obj); } } }; // 开启线程去访问WebService executorService.submit(new Runnable() { @Override public void run() { SoapObject resultSoapObject = null; Message mgs = mHandler.obtainMessage(); try { httpTransportSE.call(namespace + methodName, soapEnvelope); if (soapEnvelope.getResponse() != null) { // 获取服务器响应返回的SoapObject resultSoapObject = (SoapObject) soapEnvelope.bodyIn; } mgs.what = 0; mgs.obj = resultSoapObject; } catch (Exception e) { mgs.what = 1; mgs.obj = e.getMessage(); } // 将获取的消息利用Handler发送到主线�? mHandler.sendMessage(mgs); } }); } /** * WebService回调接口 * */ public interface WebServiceCallBack { public void onSucced(SoapObject result); public void onFailure(String result); }}

 

转载于:https://www.cnblogs.com/yico/p/4993786.html

你可能感兴趣的文章
【Linux高级驱动】I2C驱动框架分析
查看>>
赵雅智:js知识点汇总
查看>>
二维有序数组查找数字
查看>>
20个Linux服务器性能调优技巧
查看>>
多重影分身:一套代码如何生成多个小程序?
查看>>
Oracle将NetBeans交给了Apache基金会
查看>>
填坑记:Uncaught RangeError: Maximum call stack size exceeded
查看>>
SpringCloud之消息总线(Spring Cloud Bus)(八)
查看>>
DLA实现跨地域、跨实例的多AnalyticDB读写访问
查看>>
实时编辑
查看>>
KVO原理分析及使用进阶
查看>>
【348天】每日项目总结系列086(2018.01.19)
查看>>
【JS基础】初谈JS现有的数据类型
查看>>
【294天】我爱刷题系列053(2017.11.26)
查看>>
Microsoft发布了Azure Bot Service和LUIS的GA版
查看>>
Google发布Puppeteer 1.0
查看>>
.NET开源现状
查看>>
可替换元素和非可替换元素
查看>>
2016/08/25 The Secret Assumption of Agile
查看>>
(Portal 开发读书笔记)Portlet间交互-PortletSession
查看>>