问题描述
这是我的Android应用程序,我需要使用返回我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?
推荐答案
我从链接添加样本,并添加我的评论里面它:
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;
}
这篇关于替代JSON对象的Android应用程序绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!