android上的inputstream - 大json无法读取所有数据

时间:2015-11-19 09:01:58

标签: android json

Android Studio 1.5

我已经构建了一个我复制到Android Studio的java库。在我的Linux上运行的测试用例中,java库可以正常运行。但是,当我将库复制到Android工作室并在实际设备上进行测试时。它无法读取所有JSON。只读了一半。 JSON只有大约15238个字节。但是,我有其他JSON,它们大约1000字节,可以使用此代码。

Android上有某种限制吗? 我使用的是httpUrlConnectionPOST

jsonString = readJSONInputStream(mHttpUrlconnection.getInputStream());

功能:

private String readJSONInputStream(InputStream inputStream) {
   try {
        final int SIZE = 8024;
        reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), SIZE);           
        String line = "";
        String jsonString = "";

        while((line = reader.readLine()) != null) {
            jsonString += line;
        }
        log.log(Level.INFO, "JSONSTRING: " + jsonString);

        /* Success */
        return jsonString;
    }
    catch(...) {
    .
    }
}

我也尝试过使用它。哪个适用于Linux上的测试用例。但无法读取Android上的所有数据。

 private String readJSONInputStream(InputStream inputStream) {
       try {
             String jsonString = IOUtils.toString(inputStream, "UTF-8");
            /* Success */
            return jsonString;
        }
        catch(...) {
        .
        }
    }

使用HttpUrlConnection的代码

  try {
            URL url = null;
            HttpsURLConnection connection = null;
            OutputStreamWriter outputStream = null;
            int responseCode = -1;

            url = new URL(cnn_url);

            connection = (HttpsURLConnection)url.openConnection();    
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json; charset=utf8");
            connection.setRequestProperty("Accept", "application/json");
            connection.setUseCaches(true);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setConnectTimeout(15000); /* msecs i.e. 15 seconds */

            /* Send POST request with JSON object */
            outputStream = new OutputStreamWriter(connection.getOutputStream());
            outputStream.write(jsonObject.toString(), 0, jsonObject.toString().length());
            outputStream.flush();
            log.log(Level.INFO, "Connected to server OK");

            /* Get response */
            responseCode = connection.getResponseCode();
            log.log(Level.INFO, "Returned responseCode [ " + responseCode + " ]");

            String jsonString = null;

            if(responseCode == 200) {
                /* Read contents of inputstream into a string to be returned */
                jsonString = readJSONInputStream(connection.getInputStream());
                if(jsonString == null) {
                    log.log(Level.SEVERE, "Failed to read connection input stream");
                }
                else {
                    log.log(Level.INFO, "jsonString: " + jsonString);
                }
            }
            else {
                log.log(Level.SEVERE, "Invalid response code [ " + responseCode + " ]");
            }

            return jsonString;
        }

当JSON小于~1000字节时,这些函数适用于Android。但是对于一个大的JSON,我有~15000它只读〜一半。当我打印输出时,我只看到一半的JSON字符串。

我做错了吗?

1 个答案:

答案 0 :(得分:1)

输出中没有错误,只是android监视器(Logcat)有一个字符串长度限制
这意味着您的代码完美无缺

编辑()

看起来使用字符串连接并不好,它会使性能如此糟糕,,,,
尝试使用

InputStream content = entity.getContent();

//(1)

StringBuilder builder =new StringBuilder()
BufferedReader bReader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = bReader.readLine()) != null) {
    builder.append(line);
}

并且不要指定尺寸