Android调用webservice接口

前言:

WebService是一种基于SOAP协议的远程调用标准,通过webservice可以将不同操作系统平台、不同语言、不同技术整合到一块。在Android SDK中并没有提供调用WebService的库,因此,需要使用第三方的SDK来调用WebService。Android中ksoap2是WebService常用的sdk。

一、使用方法

1、工具类封装

package com.dj.webserviceapp;

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

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

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;

public class WebServiceUtils {
    // 含有3个线程的线程池
    private static final ExecutorService executorService = Executors.newFixedThreadPool(3);//限制线程池大小为3的线程池
    public static void callWebService(String url, String namespace, String methodName, HashMap properties, final WebServiceCallBack webServiceCallBack){
        //创建HttpTransportSE对象,传递WebService服务器地址
        final HttpTransportSE httpTransportSE = new HttpTransportSE(url);
        //创建SoapObject对象
        final 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 soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        //设置是否调用的是.NET开发的WebService
        soapSerializationEnvelope.setOutputSoapObject(soapObject);
        soapSerializationEnvelope.dotNet = true;
        httpTransportSE.debug = true;

        //用于子线程与主线程通信的Handler
        final Handler mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //将返回值回调到callBack的参数中
                webServiceCallBack.callBack((SoapObject) msg.obj);
            }
        };
        //开启线程去访问WebService
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                SoapObject resultSoapObject = null;
                try {
                    httpTransportSE.call(namespace + methodName,soapSerializationEnvelope);
                    if (soapSerializationEnvelope.getResponse() != null){
                        //获取服务器响应返回的SoapObject
                        resultSoapObject = (SoapObject) soapSerializationEnvelope.bodyIn;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }finally {
                    //将获取的消息通过handler发到主线程
                    mHandler.sendMessage(mHandler.obtainMessage(0,resultSoapObject));
                }
            }
        });
    }

    public interface WebServiceCallBack{
        void callBack(SoapObject result);
    }
}

从上面代码可以看出,调用webservice需要传4个参数:url,命名空间,方法名, 还有给后端的参数。

每个 XML Web services 都需要一个唯一的命名空间,以便客户端应用程序能够将它与 Web 上的其他服务区分开。

2、使用

public class MainActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);
    }

    String url = "xxxxxxxxxxxxxxxxxxx";
    String nameSpace = "xxxxxxxxxxxxxxx";
    public void clickBt(View v) {
//        HashMap properties = new HashMap();
//        properties.put("params1", "xxxxx");
        //如果有请求字段则写properties,否则将properties至为null即可
        WebServiceUtils.callWebService(url, nameSpace, "GetStockList", null, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                if (result != null) {
                    Log.d("result",result.toString());
                }
            }
        });
    }
}

demo:https://gitee.com/helloworldjie/WebServiceApp

 

你可能感兴趣的:(Android调用webservice接口)