简体   繁体   English

在Android中调用网络服务以获取byte []

[英]Calling a web service to obtain an byte[] in Android

I've writing a application on Android which calls a .Net restful wcf web service I've written. 我已经在Android上编写了一个应用程序,该应用程序调用了我编写的.Net Restful WCF Web服务。 I'm successfully obtaining XML and String results, but cannot call a method on the web service which returns a byte[] - it looks like its encoding is alerted. 我已成功获取XML和String结果,但无法在Web服务上调用返回byte []的方法-似乎已警告其编码。

I am using the org.apache.http.HttpHost library and an currently reading the byte[] as a string (the encoding in eclipse looks like unicode); 我使用的是org.apache.http.HttpHost库,并且当前正在读取byte []作为字符串(eclipse中的编码看起来像unicode);

     InputStream in = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) != null){
            str.append(line);
        }

        in.close();
        String asciiString = str.toString();

Basically I want to convert the data held in asciiString to a Bitmap (the WCF service outputs a byte[] of a PNG image) 基本上,我想将asciiString中保存的数据转换为位图(WCF服务输出PNG图像的byte [])

If anyone can offer pointers as what I am doing wrong or how to do this (if it is possible) it would be most welcome. 如果有人可以提供我在做什么或如何做(如果可能的话)的指针,那将是非常受欢迎的。

Many thanks 非常感谢

I believe you should use a ByteArrayOutputStream . 我相信您应该使用ByteArrayOutputStream I don't have an IDE installed here, but here's how I think it should go: open a ByteArrayOutputStream , to which you write byte by byte (or better, buffering in some way, of course) what you read from your InputStream in . 我没有在这里安装IDE,但是我认为应该这样做:打开ByteArrayOutputStream ,在其中逐个字节地写(或者,以某种方式缓冲,当然)从InputStream in读取InputStream in To obtain your byte[] use the method toByteArray() . 要获取byte[]使用toByteArray()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM