从SOAP服务传输的字节数组到Android [英] Transferring byte array from soap service to android

查看:153
本文介绍了从SOAP服务传输的字节数组到Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android客户端发出请求到SOAP服务。 SOAP服务读取的图像转换为一系列字节,并返回一个字节数组(相当大的)。这会被看作是一个原始的传输类型?我想问的原因是因为我有服务code读取图像并打印前5个字节。然后返回字节数组。

I have an android client that makes a request to a soap service. The soap service reads an image into a series of bytes and returns a byte array (quite a large one). Would this be seen as a primitive transfer type? The reason I ask is because I have the service code that reads an image and prints the first 5 bytes. Then it returns the byte array.

@Override
public byte[] getImage() {
    byte[] imageBytes = null;
    try {
        File imageFile = new File("C:\\images\\car.jpg");
        BufferedImage img = ImageIO.read(imageFile);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
        ImageIO.write(img, "jpg", baos);
        baos.flush();
        imageBytes = baos.toByteArray();
        baos.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    System.out.println("Got request");
    System.out.println("****** FIRST 5 BYTES: ");
    for(int i=0; i<5; i++) {
        System.out.println("****** " + imageBytes[i]);
    }
    return imageBytes;
}

这是该服务的服务器上的输出是

The output from the service on the server is

****** FIRST 5 BYTES: 
****** -119
****** 80
****** 78
****** 71
****** 13

当我收到这对我的Andr​​oid模拟器,我打印了前5个字节,它们是完全不同的那些印在服务器上。这是我的Andr​​oid code:

When I receive this on my android emulator, I print the first 5 bytes and they are completely different to those printed on the server. Here is my android code:

        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
        String result = resultsRequestSOAP.toString();
        System.out.println("****** RESULT: " + result);
        byte[] b = result.getBytes();
        System.out.println("****** FIRST 5 BYTES: ");
        for(int i=0; i<5; i++) {
            System.out.println("****** " + b[i]);
        }

和输出

****** FIRST 5 BYTES: 
****** 105
****** 86
****** 66
****** 79
****** 82

它工作正常,如果我用Java编写一个简单的服务客户端进行测试。任何想法,为什么这可能发生?

It works fine if I test it with a simple service client written in java. Any ideas why this might be happening?

推荐答案

看样子,你缺少一个重要的解码步骤。的code表示生成SOAP响应必须被编码字节数组,以便它可以被重新psented作为字符串在XML $ P $。这很难说,你应该如何对其进行解码不知道在服务器端(使用的编码机制BASE64等)。

It appears that you're missing an important decoding step. The code that generates the SOAP response has to be encoding the byte array so that it can be represented as a string in the XML. It's difficult to say how you should be decoding it without knowing the encoding mechanism used on the server side (base64, etc.).

在你的code以上您所呼叫串#的GetBytes(),该连接codeS给定的字符串使用默认字符集(这是UTF-8在Android)的二进制数据。这是不一样的原始图象数据进行解码。 UTF-8不能用来作为通用编码机制,因为它不能重新present字节任意序列(不是所有的字节序列是有效的UTF-8)

In your code above you are calling String#getBytes(), which encodes the given string as binary data using the default character set (which is UTF-8 on Android). This is not the same as decoding the original image data. UTF-8 could not be used as a general purpose encoding mechanism since it cannot represent any arbitrary sequence of bytes (not all bytes sequences are valid UTF-8).

检查服务code,看看它是如何编码的二进制数据,并在客户端使用适当的解码机制,它应该解决您的问题。

Check the service code to see how it is encoding the binary data and use an appropriate decoding mechanism on the client side, and it should resolve your issue.

HTH。

修改此常见问题解答(对于较旧的版本,但可能仍然适用)建议编码的字节数组作为Base64和包装在一个SoapPrimitive。我个人使用共享codeC 及其Base64的等级,符合EN code字节数组作为服务器端的字符串,并用它在客户端脱$ C C此字符串$回到原来的字节数组。

EDIT: This faq (for an older version but likely still applies) recommends encoding the byte array as base64 and wrapping it in a SoapPrimitive. Personally I would use Commons Codec and its Base64 class to encode the byte array as a string on the server side and use it on the client side to decode this string back to the original byte array.

有一点要注意... Android的实际捆绑的共享codeC的旧版本,这样可能你绊倒。尤其是,它不包括连接codeBase64String 方便的方法,所以你需要进行试验或做一些研究,以找出解码方法,在Android平台上存在哪些。您可以使用 android.util.Base64 在客户端上,但要确保使用与您使用在服务器端的编码风格,正确的标志。祝你好运。

One thing to watch out for... Android actually bundles an older version of Commons Codec so that might trip you up. In particular it does not include the encodeBase64String convenience method, so you'll need to experiment or do some research to find out what decoding methods exist on the Android platform. You can use android.util.Base64 on the client side but make sure to use the correct flags that match the encoding style you used on the server side. Good luck.

这篇关于从SOAP服务传输的字节数组到Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆