Android 解析网络流工具类

2017-06-24  本文已影响0人  Master_Yang

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

猜你喜欢

热点阅读