【问题标题】:inputstream on android - large json fails to read all dataandroid上的输入流-大型json无法读取所有数据
【发布时间】:2015-11-19 19:44:35
【问题描述】:
Android Studio 1.5

我已经构建了一个 java 库,我将其复制到 Android Studio 中。 java 库在我在 Linux 上运行的测试用例中运行正常。但是,当我将库复制到 Android Studio 并在实际设备上进行测试时。它无法读取所有 JSON。只有 ~half 被读取。 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 上运行。但是对于我拥有的 ~15000 的大型 JSON,它只能读取 ~ 一半。当我打印输出时,我只看到一半的 JSON 字符串。

是不是我做错了什么?

【问题讨论】:

  • 点击这个链接你总是将大数据发送到服务器使用 MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("头像", new ByteArrayBody(data,"pic.jpg" )); stackoverflow.com/questions/14027882/…
  • 我没有发送大数据。但是收到一个大的 JSON。

标签: android json


【解决方案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);
}

并且不要指定大小

【讨论】:

  • S.阿拉瓦迪。我认为情况并非如此。因为将返回 json 字符串进行解析。解析器总是失败,因为 JSON 是无效的,因为它丢失了一半。
  • S.Alawadi,感谢您的帮助。但是 sn-p 对我不起作用。它无法读取任何内容。我已经使用用于连接的代码编辑了我的问题。 entity.getContent() 返回一个对象而不是输入流。我说的对吗?
  • 你能在浏览器中读取json吗,如果可以的话,把结果放到Online JsonViewer中,这样可以帮助你知道json是否已经有无效结构跨度>
  • 是的,我可以使用 firefox 插件检索 JSON。当从 Linux 上运行的 java 测试用例中检索到 JSON 时,该 JSON 是有效的。但是,在 Android 上运行会产生无效,因为 JSON 不完整。有没有办法证明输入流确实包含所有的 JSON?也许这是一个阅读问题?
  • 你对 logcat 的看法是对的。这就是问题所在。
猜你喜欢
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2019-07-26
  • 2014-12-13
  • 1970-01-01
  • 2014-12-04
相关资源
最近更新 更多