• ksoap2 android 调用WebService


    webService,soap,wsdl的基本概念?

    详情请看维基百科

    基于soap 1.1, soap 1.2 的请求和响应数据源

    查找了很久都是基于json格式传输数据,但是最终还是找到了基于xml webservice的接口 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx,里面提供了一些省市天气查询,国家名称以及对应id查询等。

    ksoap2 调用WebService

    1:要在android上调用,我们首先下载ksoap2-android jar包,丢到lib包下,添加依赖

    2:接着我们提取出一个请求的工具类SoapConfig,SoapConfig 的全部代码如下

    import org.ksoap2.HeaderProperty;
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    
    import java.util.ArrayList;
    import java.util.Map;
    import java.util.Set;
    
    import io.reactivex.Single;
    import io.reactivex.android.schedulers.AndroidSchedulers;
    import io.reactivex.schedulers.Schedulers;
    import issue.cn.net.wanji.issueneimeng.network.ApiService;
    
    public class SoapConfig {
        public static final String WEB_SERVER_URL = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";
        public static volatile HttpTransportSE httpTransportSE = null;
        public static volatile SoapSerializationEnvelope envelope = null;
        public static SoapObject soapObject = null;
    
    
        // 命名空间
        public static final String NAME_SPACE = "http://webservice.eisi.hgsoft.com";
    
        public static final String NAME = "EISIService";
    
    
        public static HttpTransportSE getHttpTransportSE() {
            if (httpTransportSE == null) {
                httpTransportSE = new HttpTransportSE(ApiService.URL);
            }
            return httpTransportSE;
        }
    
    
        public static SoapObject getSoapObject() {
            soapObject = null;
            soapObject = new SoapObject(NAME_SPACE, NAME);
            return soapObject;
        }
    
        public static Single sendData(Map<String, Object> queryParamsMap, Map<String, Object> headerParamsMap) {
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
            SoapObject request = getSoapObject();
    
            //添加请求参数
            if (queryParamsMap != null && queryParamsMap.size() > 0) {
                Set<String> keys = queryParamsMap.keySet();
                for (String queryKey : keys) {
                    Logger.i("参数");
                    Logger.i(queryKey + ":" + queryParamsMap.get(queryKey));
                    request.addProperty(queryKey, queryParamsMap.get(queryKey));
                }
            }
            envelope.bodyOut = request;
            return Single.create(emitter -> {
                SoapConfig.getHttpTransportSE().call(null, envelope, addHeadProperty(headerParamsMap));
                if (envelope.getResponse() != null) {
                    emitter.onSuccess(envelope);
                } else {
                    emitter.onError(new Throwable("请求失败"));
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    
    
        /**
         * 添加头部请求参数
         *
         * @param headerParamsMap
         * @return
         */
    
        public static ArrayList<HeaderProperty> addHeadProperty(Map<String, Object> headerParamsMap) {
            ArrayList<HeaderProperty> headerProperty = new ArrayList<>();
            //添加head参数
            if (headerParamsMap != null && headerParamsMap.size() > 0) {
                Set<String> keys = headerParamsMap.keySet();
                for (String headerKey : keys) {
                    Logger.i("参数");
                    Logger.i(headerKey + ":" + headerParamsMap.get(headerKey));
    //                request.addProperty(headerKey, headerParamsMap.get(headerKey));
                    headerProperty.add(new HeaderProperty(headerKey, (String) headerParamsMap.get(headerKey)));
    
                }
            }
            return headerProperty;
        }
    
    
    }

    3:我们在ViewModel中调用请求

            Map<String, Object> headParamsMap = new HashMap<>();
            headParamsMap.put("XXX","XXX");
            headParamsMap.put("XXX","XXX");
            headParamsMap.put("XXX","XXX");
            headParamsMap.put("XXX","XXX");
            headParamsMap.put("XXX","XXX");
            
           
            Map<String, Object> queryParamsMap = new HashMap<>();
            queryParamsMap.put("XXX","XXX");
            queryParamsMap.put("XXX","XXX");
            queryParamsMap.put("XXX","XXX");
            queryParamsMap.put("XXX","XXX");
    
    
            homeResource.searchCusEvent(queryParamsMap, headParamsMap).subscribe(new SingleObserver() {
                @Override
                public void onSubscribe(Disposable d) {
                }
    
                @Override
                public void onSuccess(Object o) {
                    SoapSerializationEnvelope envelope = (SoapSerializationEnvelope) o;
                    //服务器返回的结果
                    Logger.i(envelope.getResponse());
                }
    
                @Override
                public void onError(Throwable e) {
                    Logger.i(e.getMessage());
                }
            });
            
            
    
       public Single searchCusEvent(Map<String, Object> queryParamsMap, Map<String, Object> headerParamsMap) {
            return SoapConfig.sendData(queryParamsMap, headerParamsMap);
        }

     4 解析请求WebService返回SoapObject 数据解析,我们可以按照约定的格式解析,虽然不太方便,但是暂时也没找到更好的资料或研究出更好的方法

    try {
                        SoapSerializationEnvelope envelope = (SoapSerializationEnvelope) o;
                        Object ob = envelope.bodyIn;
                        SoapObject soapObject = (SoapObject) envelope.getResponse();
                        SoapObject str = (SoapObject) soapObject.getProperty(0);
                        SoapObject st1 = (SoapObject) soapObject.getProperty(1);
                        SoapObject str2 = (SoapObject) st1.getProperty(0);
                        List<SimpleType> lists = new ArrayList<>();
    
    
                        for (int i = 0; i < str2.getPropertyCount(); i++) {
                            SoapObject soapObject1 = (SoapObject) str2.getProperty(i);
                            lists.add(new SimpleType(Integer.parseInt(soapObject1.getPropertyAsString("RegionID")), soapObject1.getPropertyAsString("RegionName")));
                        }
                        Logger.d(String.valueOf(lists.size()));
                    } catch (SoapFault soapFault) {
                        soapFault.printStackTrace();
                    }

     

  • 相关阅读:
    C++自定义一个foreach宏,偷偷懒
    线程池介绍与示例
    iOS消息中心与传感
    new 和 malloc 申请内存失败的区别处理
    iOS调试技巧——当程序崩溃的时候怎么办
    随机数原理
    iOS麦克风运用——腾讯微博“吹一吹”
    个人技术博客
    SDN第一次作业
    GitKraken 团队项目使用教程
  • 原文地址:https://www.cnblogs.com/bdsdkrb/p/9258099.html
Copyright © 2020-2023  润新知