Android Ksoap 调用webservice 获取二进制数据 byte[] 方法

来源:互联网 发布:乐山广电网络客服电话 编辑:程序博客网 时间:2024/05/18 18:14

由于Ksoap 调用远程方法返回的结果是 String

即便远程服务器返回方法是 byte[] 数据,

       但是经过 ksoap 返回的是仍String,如果我们将返回的 String 直接转换为 byte[]后发现结果是不正确的 或者无法强制转换。

我们只需要简单的2步就可以进行结果的 正确强制转换:

1.远程调用前对 envelope 对象进行MarshalBase64 注册

     2.获取结果后 对结果进行Base64解码编码


代码如下:

private static byte[] serviceBinRCP(SoapObject soapObject,String action) {        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);        envelope.setOutputSoapObject(soapObject);        new MarshalBase64().register(envelope);//1.远程调用前对 envelope 对象进行MarshalBase64 注册        HttpTransportSE se = new HttpTransportSE(url);        Object result = null;        byte []image = null;        try {            se.call(action,envelope);            result = envelope.getResponse();            if (result != null) {                image = Base64.decode(result.toString());//2.获取结果后 对结果进行Base64解码编码            }        } catch (Exception e) {            return null;//todo throw yourException        }        return image;    }


0 0