【问题标题】:Slow Performance on Android implementing Offline Dictionary using JsonReader使用 JsonReader 实现离线字典的 Android 性能缓慢
【发布时间】:2018-01-31 01:55:17
【问题描述】:

我正在尝试在我正在开发的电子书阅读器应用中实现离线词典。

让我解释一下这个过程。该词典不是原始应用程序的一部分,而是会在后台下载。原始字典数据是一个 24MB 长的 json,后来压缩到 6MB。

然后我下载压缩的 json 并将其提取到设备上。字典 json 有大约 150,000 个整数,我保存在数据库中。我正在使用 JsonReader 依次解析 json,在 DB 中添加每个条目。

以下是读取 json、解析每个条目并将其保存在数据库中的代码(我希望它是自我解释的)

public void readFile() throws IOException {

    File jsonInputFile = new File(DownloadClassHelper.getOfflineDictionaryDownloadPath());

    InputStream inputStream = new FileInputStream(jsonInputFile);
    JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));

    try {
        readArray(reader);
    } finally {
        reader.close();
    }
}

public void readArray(JsonReader reader) throws IOException {

    reader.beginArray();
    while (reader.hasNext()) {
        readObject(reader);
    }
    reader.endArray();
}

public void readObject(JsonReader reader) throws IOException {

    String word = null;

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("word")) {
            word = reader.nextString();
        } else if (name.equals("meanings") && reader.peek() != JsonToken.NULL) {
            readMeaningsArray(word, reader);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
}

public void readMeaningsArray(String word, JsonReader reader) throws IOException {

    reader.beginArray();
    while (reader.hasNext()) {
        readMeaningObject(word, reader);
    }
    reader.endArray();
}

public void readMeaningObject(String word, JsonReader reader) throws IOException {

    String definition = null, pos = null;

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();

        if (name.equals("definition")) {
            definition = reader.nextString();
        } else if (name.equals("pos")) {
            pos = reader.nextString();
        } else {
            reader.skipValue();
        }

    }
    reader.endObject();

    addWord(word, definition, pos);

    Log.d(TAG, word + " | " + definition + " | " + pos);
}

/**
 * Add a word to the dictionary.
 * @return true or false if failed
 */
public boolean addWord(String word, String definition, String pos) {

    ContentValues initialValues = new ContentValues();
    initialValues.put(TableDictionary.COLUMN_WORD, word);
    initialValues.put(TableDictionary.COLUMN_DEFINITION, definition);
    initialValues.put(TableDictionary.COLUMN_POS, pos);

    return CPWrapper.insert(TableDictionary.TABLE_NAME, initialValues);
}

我目前的方法面临性能问题,因为解析整个 json 需要将近 30 分钟。

在 Android 应用中实现离线字典的理想方法是什么?我需要改变我目前的方法吗?如果是,那么我应该如何在android中理想地实现这样的系统。

非常欢迎任何建议/提示。

【问题讨论】:

    标签: android performance offline-caching offlineapps jsonreader


    【解决方案1】:

    最好的方法是为您的应用程序使用预填充的数据库。因此,您可以将数据库文件添加到资产文件夹中的 apk 中,而不是下载 JSON 文件。你可以使用Android SQLiteAssetHelper

    【讨论】:

    • 这会增加应用程序的大小。向基本应用程序增加 6mb 对我来说将是巨大的。您还有其他想法吗?
    • 您可以在应用程序中包含初始数据库。然后在线下载整个数据库文件,替换原来的。
    猜你喜欢
    • 2014-08-19
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多