我要用一个InputStream从服务器端获取个JSON回应
需要做如下两件事情:
1.解析,然后把值显示到界面上
2.把这个feed保存到SD卡上的文件上

这两个任务如果一个一个做的话,没问题
  1. 用GSON实现的解析:
  2. Gson gson = new Gson();
  3. Reader reader = new InputStreamReader (myInputStream);
  4. Result result = gson.FrmJson(reader, Result.class)
  5. 复制到SD卡:
  6. FileOutputStream f (...) f.write (buffer)
复制代码

问题是,当解析完成的时候,我要把它写到SD卡上吗,但是这崩溃了,
因为InputStream已经关掉了,有什么办法能解决么?
中级工程师 3# neocai 发表于 2012-2-23 11:32:40
可以用ByteArrayOutputStream,然后再创建个新的,类似这样做:
  1. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  2. byte[] buf = new byte[1024];
  3. int n = 0;
  4. while ((n = myInputStream.read(buf)) >= 0)
  5.     baos.write(buf, 0, n);
  6. byte[] content = baos.toByteArray();
  7. InputStream is1 = new ByteArrayInputStream(content);
  8. ... use is1 ...
  9. InputStream is2 = new ByteArrayInputStream(content);
  10. ... use is2 ...
复制代码
您需要登录后才可以回帖 登录 | 注册

关于我们|手机版|Archiver|DEVDIV.COM ( 京ICP备07040843号 )  

GMT+8, 2012-5-19 16:25

Powered by DEVDIV.COM!

© 2010-2012 DEVDIV.COM Coummunity.

回顶部