您的位置:首页 > 移动开发 > Android开发

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

2014-12-14 22:23 417 查看
由于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;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: