首页 > 代码库 > 检测网络是否正常, inputstream 的转换

检测网络是否正常, inputstream 的转换


private boolean checkNetwork() {
    ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connManager.getActiveNetworkInfo() != null) {
        return connManager.getActiveNetworkInfo().isAvailable();
    }
    return false;
}

 

    // 将输入流转化为 String 型
    private static String parseInfo(InputStream inStream) throws Exception {
        byte[] data =http://www.mamicode.com/ read(inStream);
        // 转化为字符串
        return new String(data, "UTF-8");
    }


    // 将输入流转化为byte型
    public static byte[] read(InputStream inStream) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        inStream.close();
        return outputStream.toByteArray();
    }

 

 

检测网络是否正常, inputstream 的转换