从输入流中读取大数据,最后缺少一些数据

Reading large data from an inputstream some data missing at the end

Java(TM) SE Runtime Environment (build 1.7.0_45-b18)

您好,

我正在使用 httpUrlConnection 从 Web 服务中检索 json 字符串。然后我从连接

得到了 inputStream
jsonString = readJSONInputStream(mHttpUrlconnection.getInputStream());

然后我使用以下函数读取输入流以获取 JSON。

private String readJSONInputStream(final InputStream inputStream) {
    Reader reader = null;

    try {
         final int SIZE = 16024;

         char[] buffer = new char[SIZE];
         int bytesRead = 0;
         int read = 0;

         reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), SIZE);

         String line = "";
         String jsonString = "";

         while((line = reader.readLine()) != null) {
            jsonString += line;
         }

         /* Success */
         return jsonString;
    }
    catch(IndexOutOfBoundsException ex) {
        log.log(Level.SEVERE, "UnsupportedEncodingexception: " + ex.getMessage());
    }
    catch(IOException ex) {
        log.log(Level.SEVERE, "IOException: " + ex.getMessage());
    }
    finally {
        /* close resources */
        try {
            reader.close();
            inputStream.close();
        }
        catch(IOException ex) {
            log.log(Level.SEVERE, "IOException: " + ex.getMessage());
        }
    }

    return null;
}

但是,如果 json 很小,比如 600 字节,那么一切都可以。但是我有一些 JSON 大小约为 15000 字节,所以我将最大大小设置为 16024。

然而,JSON 它只读到大约 ~6511 就被切断了。

如果 JSON 很小,< 1000 字节没有问题。但是对于较大的 JSON 它只读了大约一半。

数据就在那里,因为我已经使用 http 请求插件在浏览器中对此进行了测试。

我是不是做错了什么。我应该检查的任何内容。

非常感谢您的任何建议,

问题已解决。由于记录器没有显示所有信息。毕竟不是真正的问题。