Android 解析网络流工具类

作用:解析网络返回的流 stream->string

public static String parseStream(InputStream inputStream) {
        ByteArrayOutputStream baos = null;
        try {
            int len = -1;
            byte buffer[] = new byte[1024];
            baos = new ByteArrayOutputStream();
            while ((len = inputStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
                return baos.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            closeIO(inputStream);
            closeIO(baos);
        }
        return null;
    }
    public static void closeIO(Closeable io) {
        if (io != null) {
            try {
                io.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            io = null;
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,914评论 18 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,764评论 18 399
  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 6,396评论 0 6
  • stream 流是一个抽象接口,在 Node 里被不同的对象实现。例如 request to an HTTP se...
    明明三省阅读 3,437评论 1 10
  • 我们常说,万事开头难,写文章也是如此。有一个合适的开头,会让写作者信心百倍的写下去,若开头半天都没写好,总难免心生...
    萌妈育儿记阅读 1,140评论 7 17