在android中玩转wcf

package com.cjt_pc.vehicleregulatoryestimate.utils;

import android.app.Activity;

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.LinkedHashMap;

/** * Created by cjt-pc on 2015/7/8. * Email:[email protected] */
public class SoapUtil implements SoapConfig {

    public static SoapObject postSoapRequest(String method) {
        return postSoapRequest(method, null);
    }

    public static SoapObject postSoapRequest(String method, LinkedHashMap<String, Object> properties) {
        String nameSpace = NAMESPACE;
        String url = getUrl(method);
        String soapAction = getAction(method);
        // 初始化传入参数soapObject
        SoapObject soapObject = new SoapObject(nameSpace, method);
        for (LinkedHashMap.Entry<String, Object> entry : properties.entrySet()) {
            soapObject.addProperty(entry.getKey(), entry.getValue());
        }
        // 设置版本和指定信封envelope的数据soapObject
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        envelope.bodyOut = soapObject;
        envelope.dotNet = true;
        // 采用webservice协议发送请求
        HttpTransportSE transport = new HttpTransportSE(url);
        transport.debug = true;
        try {
            transport.call(soapAction, envelope);
        } catch (final Exception e) {
            e.printStackTrace();
        }
        // 接受服务端返回的数据,取其child
        SoapObject object = (SoapObject) ((SoapObject) envelope.bodyIn).getProperty(0);
        return object == null ? null : object;
    }

    /** * 根据传回的方法名返回指定的url * * @param methodName 方法名 * @return 访问地址 */
    private static String getUrl(String methodName) {
        switch (methodName) {
            case LOGININ_METHOD:
                return URL_1;
            case GET_RW_LIST_METHOD:
                return URL_2;
            case GET_HBBZ_METHOD:
                return URL_1;
            case GET_PP_METHOD:
                return URL_2;
            case GET_CXI_METHOD:
                return URL_2;
            case GET_CXING_METHOD:
                return URL_2;
            case GET_UPLOAD_IMG_METHOD:
                return URL_3;
            case UPLOAD_PHOTO_METHOD:
                return URL_3;
            case SAVE_PGRW_METHOD:
                return URL_2;
        }
        return null;
    }

    /** * 根据传回的方法名返回指定的action * * @param methodName 方法名 * @return 访问action */
    private static String getAction(String methodName) {
        switch (methodName) {
            case LOGININ_METHOD:
                return LOGININ_ACTION;
            case GET_RW_LIST_METHOD:
                return GET_RW_LIST_ACTION;
            case GET_HBBZ_METHOD:
                return GET_HBBZ_ACTION;
            case GET_PP_METHOD:
                return GET_PP_ACTION;
            case GET_CXI_METHOD:
                return GET_CXI_ACTION;
            case GET_CXING_METHOD:
                return GET_CXING_ACTION;
            case GET_UPLOAD_IMG_METHOD:
                return GET_UPLOAD_IMG_ACTION;
            case UPLOAD_PHOTO_METHOD:
                return UPLOAD_PHOTO_ACTION;
            case SAVE_PGRW_METHOD:
                return SAVE_PGRW_ACTION;
        }
        return null;
    }
}

SoapConfig的代码我就不贴了,都是些地址配置,这个工具类有点通杀的感觉,那接下来就看看怎么用了。

public class LoginInTask extends AsyncTask<Void, Void, SoapObject> {

        String account, pwd;

        public LoginInTask(String account, String pwd) {
            this.account = account;
            this.pwd = pwd;
        }

        @Override
        protected SoapObject doInBackground(Void... params) {
            editor = preferences.edit();
            editor.putString("account", account);
            editor.apply();
            LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
            properties.put("TrueName", account);
            properties.put("Pwd", pwd);
            return SoapUtil.postSoapRequest("LoginIn", properties);
        }

        @Override
        protected void onPostExecute(SoapObject soapObject) {
            if (soapObject.getPropertyCount() == 0) {
                failedLogin();
            } else {
                // 登陆成功
                succeedLogin();
            }
        }
    }

为什么要用LinkedHashMap而不是HashMap?
貌似wcf传输要按照一定的顺序,否则不通!

为什么使用AsyncTask而不是手动开启线程?
这样模块化了代码,清晰易懂,并且与界面交互较为简单。

你可能感兴趣的:(android,WCF)