1

我有一个包含 json 文件和一些 base64 解码图像的压缩文件,我使用以下代码从中读取数据:

    JSONArray model = new JSONArray();
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(inputStream));
    ZipEntry zipEntry;
    while ((zipEntry = zis.getNextEntry()) != null) {
        if (zipEntry.getName().equals("model.json")) {
            long size = (int) zipEntry.getSize();
            if (size < 0) {
                size = 0xffffffffl + size;
            }
            byte[] buffer = new byte[(int) size];
            int read;
            while ((read = zis.read(buffer, 0, (int) size)) > 0) {
                Log.i("model", new String(buffer, "UTF-8"));
                model = new JSONArray(new String(buffer, "UTF-8"));
            }
        } else {
            long size = (int) zipEntry.getSize();
            if (size < 0) {
                size = 0xffffffffl + size;
            }
            byte[] buffer = new byte[(int) size];
            int read;
            while ((read = zis.read(buffer, 0, (int) size)) > 0) {
                Log.i("bitmap", new String(buffer, "UTF-8"));
                byte[] decodedMap = Base64.decode(new String(buffer, "UTF-8"), Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(decodedMap, 0, decodedMap.length);
                Map map = new Map(zipEntry.getName().substring(0, zipEntry.getName().indexOf(".")), bitmap, id, level);
                SharedData.maps.add(map);
            }
        }
        zis.closeEntry();
    }
    zis.close();

// 这不是问题:问题是zis.read(buffer, 0, (int) size)没有从文件中读取所有数据,所以我得到一个JSONException

编辑:

问题不在于从流中读取数据,它读取所有数据,但我认为问题在于编码这是 JSONException:

09-19 14:30:23.216: W/System.err(7524): org.json.JSONException: Unterminated array at character 2959607 of 

2959607 是字符串的长度

4

1 回答 1

0

问题出在 org.JSONArray 中,它无法解析大字符串,我用 GSon 替换了它,现在它工作正常

于 2016-09-19T13:44:43.630 回答