webservice1(队列上传 呼叫)

package com.omniteaching.otscale.system.webservices.Watcher;

import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import com.omniteaching.otscale.model.webservices.WebServicesRequest;
import com.omniteaching.otscale.model.webservices.WebServicesRespone;
import com.omniteaching.otscale.system.webservices.Helper;
import com.omniteaching.otscale.utils.Constants;
import com.omniteaching.otscale.utils.Globals;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public abstract class WatcherScaleWebService
{
    private static final String TAG = WatcherScaleWebService.class.getSimpleName();

    private static String WSDL_URL = "";
    private static String NAME_SPACE = "";

    public static final String NULL_CONTENT = "";
    public static final String NEW_LINE = "\n"; // =="0d0a"

    protected static final String state_failure = "0";  // 成功
    protected static final String state_success = "1";  // 失败
    protected static final String state_none_order_code = "2";  // 单据号不存在
    protected static final String state_none_inv_code = "3";  // 存货编码不存在

    protected static final String mDetailSplitRegular = ",";
    protected static final String mDefaultCharacterEncode = "UTF-8";
    protected static final int mDefaultBase64Encode = Base64.DEFAULT;

    public static final int STATE_FAILURE = 0; // 失败
    public static final int STATE_SUCCESS = 1; // 成功
    public static final int STATE_NONE_ORDER_CODE = 2; // 单据号不存在
    public static final int STATE_NONE_INV_CODE = 3; // 存货编码不存在

    private static Helper mHelper;

    public static final Map, Integer> resultCodeMap = new HashMap, Integer>();

    static {
        resultCodeMap.put(state_failure, STATE_FAILURE);
        resultCodeMap.put(state_success, STATE_SUCCESS);
        resultCodeMap.put(state_none_inv_code, STATE_NONE_INV_CODE);
        resultCodeMap.put(state_none_order_code, STATE_NONE_ORDER_CODE);
    }

    private static String mWsdlUrl = "";

    public WatcherScaleWebService() {
        mHelper = new Helper();
    }

    /**
     * 呼叫Webservice(无参数) 若呼叫无错误返回,则调用getResponeData()获取返回数据。
     *
     * @param method 呼叫的函数名
     */
    protected Object callServer(String method) {
        return call(method);
    }

    /**
     * 呼叫Webservice(带参数) 若呼叫无错误返回,则调用getResponeData()获取返回数据。
     *
     * @param method 呼叫的函数名
     * @param params 参数集合 参数Map的内容: Stringkey1 形参名1 -- value 数据 Stringkey2 形参名2 --
     *               value 数据 Stringkey3 形参名3 -- value 数据 ...
     */
    protected Object callServer(String method, Map, Object> params) {
        SoapObject soapObject = new SoapObject(getNameSpace(), method);
        if (params != null && params.size() > 0) {
            Map, Object> paramValueMap = new HashMap, Object>(params);
            Set keySet = paramValueMap.keySet();
            for (String key : keySet) {
//                Object contentObject = encode(paramValueMap.get(key));
                soapObject.addProperty(key, paramValueMap.get(key));
            }
        }
        WebServicesRequest request = new WebServicesRequest();
        request.setMethod(method);
        request.setWsdl(getWsdlUrl());
        request.setNameSpace(getNameSpace());
        request.setSoapObject(soapObject);

        return execute(request);
    }


    /**
     * @param method
     * @return Object
     */
    private final Object call(String method) {
        SoapObject soapObject = new SoapObject(getNameSpace(), method);
        WebServicesRequest request = new WebServicesRequest();
        request.setSoapObject(soapObject);
        request.setWsdl(getWsdlUrl());
        request.setNameSpace(getNameSpace());
        request.setMethod(method);
        return execute(request);
    }

    /**
     * 呼叫WebService
     *
     * @param request
     * @return
     */
    private final WebServicesRespone execute(WebServicesRequest request) {
        if (!TextUtils.isEmpty(request.getMethod())) {
            final SoapObject soap = request.getSoapObject();
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.bodyOut = soap;
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(soap);
            HttpTransportSE transportSE = new HttpTransportSE(request.getWsdl(), 10000);

            WebServicesRespone respone = new WebServicesRespone();
            try {
                String action = request.getNameSpace() + request.getMethod();
                Log.e(TAG, "sendDataWebThread omswatcher execute: "+request.getWsdl() + " "+action );
                // 呼叫WebService
                transportSE.call(action, soapEnvelope);
                // 返回数据
                Object retData = soapEnvelope.getResponse();
                respone.setValue(retData);
                respone.setResult(STATE_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                respone.setResult(STATE_FAILURE);
                respone.setDesc(e.getMessage());
                Log.e(TAG, "sendDataWebThread Exception Exception: "+e.getMessage() );
            }
            return respone;
        }
        return null;
    }

    protected boolean isErrorMessage(Object result) {
        return false;
    }

    /**
     * 将Webservice回传的原始数据解码还原但不做进一步处理。 解码出来的数据是原始明文数据。
     */
    protected Object decodeRespone(Object raw) {
        if (raw != null) {
            Object obj = raw;
            String content = obj.toString();
            String resultString = decodeBase64(content);
            // TODO 临时处理返回""数据的方法
            if (resultString.length() == 4) {  // 4:空数据乱码字符串的长度resultString.length
                resultString = NULL_CONTENT;
            }
            return resultString;
        }
        return null;
    }

    protected Object encode(Object contentObj) {
        String inputString = (String) contentObj;
        String encodeString = "";
        encodeString = encodeToBase64String(inputString);
        return encodeString;
    }


    /**
     * 将String转为Base64编码格式String
     *
     * @param stringContent
     * @return 编码成功返回 base64 String, 失败返回null。
     */
    protected String encodeToBase64String(String stringContent) {

        byte[] utf8Data = null;
        byte[] base64bytes = null;
        String strData = ""; // 最终编码后要送出的资料

        try {
            // 得到UTF-8编码的byte[]
            utf8Data = stringContent.getBytes(mDefaultCharacterEncode);
            // 将byte[]编码为BASE64数据
            base64bytes = Base64.encode(utf8Data, mDefaultBase64Encode);
            // 将编码为BASE64后的数据转成String
            strData = new String(base64bytes, mDefaultCharacterEncode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return strData;
    }

    /**
     * 解码Base64格式数据
     *
     * @param stringContent stringContent
     * @return 解码成功返回解码后的 byte[]数据,失败返回原数据。
     */
    protected String decodeBase64(String stringContent) {
        String input = stringContent;
        String base64Data = "";

        if (stringContent != null) {
            try {
                byte[] baseBytes = Base64.decode(input,
                        mDefaultBase64Encode);

                base64Data = new String(baseBytes, mDefaultCharacterEncode);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                return input;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return input;
            }
        }

        return base64Data;
    }

    public static String setWsdlUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return "";
        }

        String tmpUrl = url;

        if (!tmpUrl.startsWith("http://")) {
            tmpUrl = "http://" + tmpUrl;
        }
        if (tmpUrl.endsWith("/")) {
            tmpUrl = tmpUrl + Constants.EASY_WEBSERVICES;
        }
        if (!tmpUrl.endsWith("/" + Constants.EASY_WEBSERVICES)) {
            tmpUrl = tmpUrl + "/" + Constants.EASY_WEBSERVICES;
        }

        mWsdlUrl = tmpUrl;

        Globals.log(TAG, "WatcherScaleWebService url:" + mWsdlUrl);
        Globals.log(TAG, "WatcherScaleWebService name:" + Constants.EASY_NAMESPACE);

        setWsdl(mWsdlUrl);
        setNamespace(Constants.EASY_NAMESPACE);

        return mWsdlUrl;
    }

    private boolean check() {
        if (TextUtils.isEmpty(getWsdlUrl()) || TextUtils.isEmpty(getNameSpace())) {
            return false;
        }
        return true;
    }

    protected final static String setWsdl(String wsdl) {
        WSDL_URL = wsdl;
        return WSDL_URL;
    }

    protected final static String setNamespace(String namespace) {
        NAME_SPACE = namespace;
        return NAME_SPACE;
    }

    protected static String getWsdlUrl() {
        return WSDL_URL;
    }

    protected static String getNameSpace() {
        return NAME_SPACE;
    }


}

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