替代JSON对象的Andr​​oid应用程序绑定绑定、应用程序、对象、JSON

2023-09-05 04:47:39 作者:琴弦断丶冷笛残

这是我的Andr​​oid应用程序,我需要使用返回我JSON格式的对象列表的RESTful Web服务。 这个列表可能会很长(约1000/2000对象。)。

From my Android application I need to use a RESTful web service that returns me a list of objects in json format. This list can be very long (about 1000/2000 object.).

我需要做的是搜索和retrive只是一些对象的JSON文件中。

What I need to do is to search and retrive just some of the objects inside the json file.

由于移动设备的内存有限,我的想法是使用对象结合(例如使用GSON库)可能是危险的。

Due to the limited memory of mobile device, I was thinking that using object-binding (using for example GSON library) can be dangerous.

这是解决这个问题的办法吗?

Which are the alternatives for solving this problem?

推荐答案

如果您使用的是GSON,使用GSON流。

我从链接添加样本,并添加我的评论里面它:

I've added the sample from the link and added my comment inside of it:

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;
}