美文网首页
Android 解析网络流工具类

Android 解析网络流工具类

作者: Master_Yang | 来源:发表于2017-06-24 18:24 被阅读0次

作用:解析网络返回的流 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;
        }
    }

相关文章

网友评论

      本文标题:Android 解析网络流工具类

      本文链接:https://www.haomeiwen.com/subject/ydbkcxtx.html