Android 调用 .NET WebService

1、下载并导入jar工具类包

打开下载界面http://simpligility.github.io/ksoap2-android/getting-started.html ,拉倒最下

  • 2、Copy 到libs 文件夹下

3、使用

  /**
       * 调用WebService方法
       * @param methodName 方法名
       * @param parameters  参数map
       * @return 方法返回值
       */
        public static String getDataFromWebService(final String methodName, final Map<String, String> parameters) {

        String WSDL_URI = "http://192.168.1.133:8089/WSNBA.asmx?WSDL";//WSDL 的 uri
        String namespace = "http://tempuri.org/"; //namespace

        SoapObject request = new SoapObject(namespace, methodName);

        //添加参数
        if (parameters != null) {
            for (Map.Entry<String, String> item : parameters.entrySet()) {
                request.addProperty(item.getKey(), item.getValue());
            }
        }

        //创建SoapSerializationEnvelope 对象,同时指定soap版本号(之前在wsdl中看到的)
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER12);
        envelope.bodyOut = request;//由于是发送请求,所以是设置bodyOut
        envelope.dotNet = true;//由于是.net开发的webservice,所以这里要设置为true

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);

        try {
            httpTransportSE.call(null, envelope);//调用
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 获取返回的数据
        SoapObject object = (SoapObject) envelope.bodyIn;
        // 获取返回的结果
        return object.getProperty(0).toString();
    }
posted @ 2017-08-02 16:35  -Tiger  阅读(356)  评论(0编辑  收藏  举报