Android中调用Web服务获取byte[]
我在 Android 上编写了一个应用程序,它调用我编写的 .Net Restful wcf Web 服务。我成功获取了 XML 和 String 结果,但无法调用 Web 服务上返回 byte[] 的方法 - 看起来其编码已收到警报。
我正在使用 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();
基本上我想将 asciiString 中保存的数据转换为位图(WCF 服务输出 PNG 图像的 byte[])
如果有人可以提供指针作为我做错了什么或如何做到这一点(如果可能的话)将是最受欢迎的。
非常感谢
I've writing a application on Android which calls a .Net restful wcf web service I've written. 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.
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);
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)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信你应该使用
ByteArrayOutputStream
。我这里没有安装 IDE,但我认为应该这样做:打开一个 ByteArrayOutputStream,向其中逐字节写入(或者更好,当然以某种方式缓冲)您从InputStream in
中读取。要获取byte[]
,请使用方法toByteArray()
。I believe you should use a
ByteArrayOutputStream
. I don't have an IDE installed here, but here's how I think it should go: open aByteArrayOutputStream
, to which you write byte by byte (or better, buffering in some way, of course) what you read from yourInputStream in
. To obtain yourbyte[]
use the methodtoByteArray()
.