我一直在使用GSON從AppEngine上JSON對象運到我的Android客戶端,沒有問題。 我正在使用下面的代碼。未終止的字符串錯誤與GSON
服務器:
Query<WaterSupply> q = ofy.query(WaterSupply.class).filter("FdID", fdID)
.filter("timeUpdated >", lastUpdate);
Gson gson=new Gson();
resp.getWriter().print(gson.toJson(q.list()));
客戶端:
List<WaterSupply> list=new ArrayList<WaterSupply>();
String json1 =wsResult.wsResponse;
JsonElement json = new JsonParser().parse(json1);
JsonArray array= json.getAsJsonArray();
Iterator<JsonElement> iterator = array.iterator();
while(iterator.hasNext()){
JsonElement json2 = (JsonElement)iterator.next();
Gson gson = new Gson();
WaterSupply ws= gson.fromJson(json2, WaterSupply.class);
//can set some values in contact, if required
list.add(ws);
}//Unable to invoke no-args constructor for interface com.jackson.FirefighterLog.shared.WaterSupplyProxy.
//Register an InstanceCreator with Gson for this type may fix this problem.
Gson gson = new Gson();
Type listType = new TypeToken<List<WaterSupply>>(){}.getType();
List<WaterSupply> wsList = (List<WaterSupply>) gson.fromJson(wsResult.wsResponse, listType);
我一直在使用較大的物體開始,不得不切換到流解析實現,因爲我得到了內存不足的錯誤。我現在用下面的代碼:
服務器: 同上
客戶端:
public static List<WaterSupply> readJsonStream(InputStream in) throws IOException {
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
Gson gson=new Gson();
List<WaterSupply> messages = new ArrayList<WaterSupply>();
reader.beginArray();
while (reader.hasNext()) {
WaterSupply message = gson.fromJson(reader, WaterSupply.class);
messages.add(message);
}
reader.endArray();
reader.close();
return messages;
}
我假定這事做用換行符(/ N),但我不確定在這一點上,不知道如何解決這個...任何想法?
我想你的建議,我得到了同樣的錯誤。服務器端看起來不錯,它似乎是換行符的問題。我曾嘗試與服務器上的「\\ N」代替「\ n」,但我得到在App Engine上一deadlineexceeded例外(需要長期)... – Patrick 2012-07-08 17:02:19