gpt4 book ai didi

java - Android 应用程序中 JSON 对象绑定(bind)的替代方案

转载 作者:行者123 更新时间:2023-12-01 12:20:41 24 4
gpt4 key购买 nike

在我的 Android 应用程序中,我需要使用 RESTful Web 服务,该服务返回 json 格式的对象列表。这个列表可能会很长(大约 1000/2000 个对象)。

我需要做的是搜索并检索 json 文件中的一些对象。

由于移动设备的内存有限,我认为使用对象绑定(bind)(例如使用 GSON 库)可能很危险。

解决这个问题有哪些替代方案?

最佳答案

If you are using gson, use gson streaming .

我已从链接添加了示例,并在其中添加了我的评论:

public List<Message> readJsonStream(InputStream in) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
List<Message> messages = new ArrayList<Message>();
reader.beginArray();
while (reader.hasNext()) {
Message message = gson.fromJson(reader, Message.class);
// TODO : write an if statement
if(someCase) {
messages.add(message);
// if you want to use less memory, don't add the objects into an array.
// write them to the disk (i.e. use sql lite, shared preferences or a file...) and
// and retrieve them when you need.
}
}
reader.endArray();
reader.close();
return messages;
}

关于java - Android 应用程序中 JSON 对象绑定(bind)的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26680656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com