如何将输入流转换为字符串

时间:2018-07-10 11:57:44

标签: android inputstream

在我的项目中,在异步任务的GET方法中,我得到了具有正确响应的输入流,

 HttpResponse httpResponse = httpclient.execute(get);

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();
if(inputStream != null){
            result = convertStreamToString(inputStream);
        }

但是,当Iam尝试将此Inputstream转换为string时,Iam获取空字符串 result =“” ,这是我的代码

 private static String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
         while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

有人能知道为什么此方法对某些api返回null,对于其他Apis我却得到了正确的响应作为输入流,但是在转换为字符串时会清空空字符串吗?

0 个答案:

没有答案