Android对接webService接口

webService接口

本文webService服务采用Java编程,Android调用采用的ksoap2-3.6.2.jar。

jar包下载链接:https://pan.baidu.com/s/1mxhF0i3Fn86hSsZA7r7oRg 密码:k57x

如:

    WEB_SERVER_URL = "http://192.168.191.1:8080/WT600WebService/wtwebservice?wsdl";
               // 命名空间
               NAMESPACE = "http://serviceimpl.app.wonder.com/";

在浏览器中分打开:

Android对接webService接口_第1张图片

说明:

targeNamespace="http://serviceimpl.app.wonder.com/":命名空间。

name="ServiceWT600ImplService":服务的名字。

1:方法名为HelloWorld的函数,无参数。

2:为HelloWorld的返回数据,返回数据为:HelloWorldResponse{return=HelloWorld}。返回的数据值为"return"对应的数据。

3:方法名为itcIsOnline的函数,需要传递类型为String的参数。传参类似与map形式的键值对,对应的key的名字为"arg0"。也就是说,在调用这个方法时,需要使用SoapObject,将参数存在arg0中。

        // 创建SoapObject对象
        SoapObject soapObject = new SoapObject(nameSpace, methodName);
        // SoapObject添加参数
        soapObject.addProperty("arg0", "上传的数据");

4:为方法itcIsOnline返回的数据,格式同2。

工具类

WebServiceUtils:

package com.example.a_test.webservice;

import java.io.IOException;
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.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Handler;
import android.os.Message;

/**
 * @author ${M.xang}
 * 创建时间:2018-8-25 上午10:00:37 
 * 类描述 :
 *		WebService工具类
 */
public class WebServiceUtils {

    // 含有3个线程的线程池
    private static final ExecutorService executorService = Executors.newFixedThreadPool(3);


    public static final String WEB_SERVER_URL = "http://192.168.191.1:8080/WT600WebService/wtwebservice?wsdl";
    // 命名空间
    private static final String NAMESPACE = "http://serviceimpl.app.wonder.com/";
	
    /**
     * @param methodName
     *            WebService的调用方法名
     * @param properties
     *            WebService的参数
     * @param webServiceCallBack
     *            回调接口
     */
    public static void callWebService(final String methodName,
            HashMap properties,final WebServiceCallBack webServiceCallBack) {
        
    	// 创建HttpTransportSE对象,传递WebService服务器地址,设置超时时间为30s
        final HttpTransportSE httpTransportSE = new HttpTransportSE(WEB_SERVER_URL, 30*1000);
        // 创建SoapObject对象
        SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
        // SoapObject添加参数,如果properties传null,表示调用无参函数
        if (properties != null) {
            for (Iterator> it = properties.entrySet()
                    .iterator(); it.hasNext();) {
                Map.Entry entry = it.next();
                // 往soapObject中保存上传的参数数据
                soapObject.addProperty(entry.getKey(), entry.getValue());
            }
        }

        // 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号
        final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        soapEnvelope.bodyOut = soapObject;
        soapEnvelope.dotNet = false;// 设置是否调用的是.Net开发的WebService,一定要设为false
        soapEnvelope.encodingStyle = "UTF-8";
        httpTransportSE.debug = true;
        soapEnvelope.setOutputSoapObject(soapObject);

        // 用于子线程与主线程通信的Handler
        final Handler mHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
			case 0:
			    // 将返回值回调到callBack的参数中
	                    webServiceCallBack.callBack((SoapObject) msg.obj,"");
			break;
			case 1:
			    // 将返回值回调到callBack的参数中
			    webServiceCallBack.callBack(null,(String) msg.obj);
			break;
			default:
			break;
		}
            }

        };

        // 开启线程去访问WebService
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                SoapObject resultSoapObject = null;
                try {
                    httpTransportSE.call(NAMESPACE+ methodName, soapEnvelope);
                    if (soapEnvelope.getResponse() != null) {
                        // 获取服务器响应返回的SoapObject
                        resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
                    }
                    // 将获取的消息利用Handler发送到主线程
                    mHandler.sendMessage(mHandler.obtainMessage(0,resultSoapObject));
                } catch (HttpResponseException e) {
                    e.printStackTrace();
                    mHandler.sendMessage(mHandler.obtainMessage(1,e.toString()));
                } catch (IOException e) {
                    e.printStackTrace();
                    mHandler.sendMessage(mHandler.obtainMessage(1,e.toString()));
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                    mHandler.sendMessage(mHandler.obtainMessage(1,e.toString()));
                }
            }
        });
    }
	
    /**
     * 数据返回结果
     */
    public interface WebServiceCallBack {
        public void callBack(SoapObject result,String errorInfo);
    }
}

说明:

soapEnvelope.dotNet = false;// 设置是否调用的是.Net开发的WebService。

如果不是.Net开发的WebService这里一定要设为false,否则会特别酸爽.......别问我为什么,我测了半天才找到问题。
       

调用方法

在Activity中的使用:

HashMap map = new HashMap();
map.put("arg0", xmlPara);
// 上传数据
WebServiceUtils.callWebService(webMethod, map, new WebServiceCallBack() {
    @Override
    public void callBack(SoapObject result, String errorInfo) {
	if(result != null){
	    Log.e("----------", result.toString());
	    String str = result.getProperty(0).toString();
	    tv_resultInfo.setText(str);
	} else {
	    if(!errorInfo.equals("")){
	        tv_resultInfo.setText(errorInfo);
	    }
	}
    }
});

说明:arg0为方法对应的参数的key(arg0为默认值,可能为其他值,如下图所示),xmlpara为要上传的数据。

Android对接webService接口_第2张图片

 

 

 

你可能感兴趣的:(android)