Android AsyncTask+Soap 的简单使用

Soap 描述如下:

基于类对象的传输协议。
SOAP封装(envelop),它定义了一个框架,描述消息中的内容是什么,是谁发送的,谁应当接受并处理它以及如何处理它们;
SOAP编码规则(encoding rules),它定义了一种序列化机制,用于表示应用程序需要使用的数据类型的实例;
SOAP RPC表示(RPC representation),它定了一个协定,用于表示远程过程调用和应答;
SOAP绑定(binding),它定义了SOAP使用哪种协议交换信息。使用HTTP/TCP/UDP协议都可以。


android 使用soap 步骤如下:

1.引用第三方库
    compile files('libs/ksoap2-android-assembly-3.6.1-jar-with-dependencies.jar')

2.创建一个工具栏方便进行网络请求AsyncTaskSoapUtil.java
package net.sytm.soapdemo.util;

import android.os.AsyncTask;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
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.Map;
import java.util.Set;

/**
 * Created by aoc on 2016/8/31.
 */

public class AsyncTaskSoapUtil extends AsyncTask<Void, Void, String> {

    private String name;
    private String url;
    private String action;
    private Map<String, String> param;
    private Callback callback;

    public AsyncTaskSoapUtil(String name, String url, String action, Map<String, String> param) {
        this.name = name;
        this.url = url;
        this.action = action;
        this.param = param;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    @Override
    protected void onPreExecute() {
        callback.onPre();
    }

    @Override
    protected String doInBackground(Void... voids) {
        String xml = "";
        SoapObject request = new SoapObject(name, action);
        Set<Map.Entry<String, String>> set = param.entrySet();
        for (Map.Entry<String, String> entry : set) {
            request.addProperty(entry.getKey(), entry.getValue());
        }
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.bodyOut = request;
        HttpTransportSE transportSE = new HttpTransportSE(url);
        try {
            transportSE.call(getSoapAction(name, action), envelope);
            SoapObject result = (SoapObject) envelope.getResponse();
            //Object bodyIn =  envelope
            xml = result.toString();
        }
        catch (SoapFault soapFault) {
            soapFault.printStackTrace();
        }
        catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return xml;
    }

    @Override
    protected void onPostExecute(String data) {
        callback.onPost(data);
    }

    public interface Callback {
        void onPre();
        void onPost(String data);
    }

    private String getSoapAction(String name, String action) {
        StringBuilder builder = new StringBuilder();
        builder.append(name);
        builder.append(action);
        return builder.toString();
    }
}

3.使用示例
    private void send(String value1, String value2) {
        //接口
        String action = "MemberLogin";
        //参数
        NameValueUtil nameValueUtil = new NameValueUtil();
        nameValueUtil.put("phone", value1);
        nameValueUtil.put("password", value2);
        AsyncTaskSoapUtil taskUtil = new AsyncTaskSoapUtil(Constant.name, Constant.url, action, nameValueUtil.getParam());
        taskUtil.setCallback(callback);
        taskUtil.execute();
    }

    AsyncTaskSoapUtil.Callback callback = new AsyncTaskSoapUtil.Callback() {
        @Override
        public void onPre() {

        }

        @Override
        public void onPost(String data) {
            resultView.setText(data);
        }
    };



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值