当我尝试从服务器获取json格式的字符串时,我得到如下错误 java.lang.OutOfMemoryError:无法分配带有16777216个空闲字节的68706640字节分配和37MB直到OOM 在java.util.ArrayList.add(ArrayList.java:118)
我的java代码如下
JSONArray eventArray = eventResult.getJSONArray("event");
for (int i=0; i<eventArray.length(); i++){
final ArrayList<String> divisi = new ArrayList<>();
final ArrayList<Timeline> timelines = new ArrayList<>();
JSONObject eventObj = eventArray.getJSONObject(i);
String namaEvent = eventObj.getString("nama_event");
String deskripsi = eventObj.getString("deskripsi");
String tanggal = eventObj.getString("tanggal");
JSONArray eventArray1 = eventObj.getJSONArray("divisi");
JSONArray eventArray2 = eventObj.getJSONArray("timeline");
for (int j=0; j<eventArray1.length(); i++){
JSONObject eventObj1 = eventArray1.getJSONObject(j);
String namaDivisi = eventObj1.getString("nama_divisi");
divisi.add(namaDivisi);
}
for (int k=0; k<eventArray2.length(); k++){
JSONObject eventObj2 = eventArray2.getJSONObject(k);
String tanggalTimeline = eventObj2.getString("tanggal_timeline");
String deskripsiTimeline = eventObj2.getString("deskripsi_timeline");
Timeline timeline = new Timeline(tanggalTimeline, deskripsiTimeline);
timelines.add(timeline);
}
Event event = new Event(namaEvent,deskripsi,tanggal,divisi,timelines);
events.add(event);
}
我要解析的json结构是
{
"error": false,
"event": [
{
"id_event": "67",
"nama_event": "Anforcom",
"deskripsi": "Lomba mobile apps nasional",
"tanggal": "2017-10-21",
"divisi": [
{
"id_divisi": "DI-67-0",
"nama_divisi": "keamanan"
},
{
"id_divisi": "DI-67-1",
"nama_divisi": "humas"
},
{
"id_divisi": "DI-67-2",
"nama_divisi": "acara"
},
{
"id_divisi": "DI-67-3",
"nama_divisi": "PDD"
},
{
"id_divisi": "DI-67-0",
"nama_divisi": "keamanan"
},
{
"id_divisi": "DI-67-1",
"nama_divisi": "humas"
},
{
"id_divisi": "DI-67-2",
"nama_divisi": "acara"
},
{
"id_divisi": "DI-67-3",
"nama_divisi": "PDD"
},
{
"id_divisi": "DI-67-0",
"nama_divisi": "keamanan"
},
{
"id_divisi": "DI-67-1",
"nama_divisi": "humas"
},
{
"id_divisi": "DI-67-2",
"nama_divisi": "acara"
},
{
"id_divisi": "DI-67-3",
"nama_divisi": "PDD"
}
],
"timeline": [
{
"id_timeline": "TL-67-0",
"tanggal_timeline": "2017-10-13",
"deskripsi_timeline": "Tes wawancara"
},
{
"id_timeline": "TL-67-0",
"tanggal_timeline": "2017-10-13",
"deskripsi_timeline": "Tes wawancara"
},
{
"id_timeline": "TL-67-0",
"tanggal_timeline": "2017-10-13",
"deskripsi_timeline": "Tes wawancara"
},
{
"id_timeline": "TL-67-0",
"tanggal_timeline": "2017-10-13",
"deskripsi_timeline": "Tes wawancara"
},
{
"id_timeline": "TL-67-1",
"tanggal_timeline": "2017-10-14",
"deskripsi_timeline": "Hasil tes wawancara"
},
{
"id_timeline": "TL-67-1",
"tanggal_timeline": "2017-10-14",
"deskripsi_timeline": "Hasil tes wawancara"
},
{
"id_timeline": "TL-67-1",
"tanggal_timeline": "2017-10-14",
"deskripsi_timeline": "Hasil tes wawancara"
},
{
"id_timeline": "TL-67-1",
"tanggal_timeline": "2017-10-14",
"deskripsi_timeline": "Hasil tes wawancara"
},
{
"id_timeline": "TL-67-2",
"tanggal_timeline": "2017-10-20",
"deskripsi_timeline": "Technical meeting"
},
{
"id_timeline": "TL-67-2",
"tanggal_timeline": "2017-10-20",
"deskripsi_timeline": "Technical meeting"
},
{
"id_timeline": "TL-67-2",
"tanggal_timeline": "2017-10-20",
"deskripsi_timeline": "Technical meeting"
},
{
"id_timeline": "TL-67-2",
"tanggal_timeline": "2017-10-20",
"deskripsi_timeline": "Technical meeting"
}
]
},
{
"id_event": "71",
"nama_event": "I-Gate",
"deskripsi": "Gathering antar angkatan mahasiswa informatika",
"tanggal": "2017-10-28",
"divisi": [
{
"id_divisi": "DI-71-0",
"nama_divisi": "Humas"
},
{
"id_divisi": "DI-71-1",
"nama_divisi": "PDD"
},
{
"id_divisi": "DI-71-2",
"nama_divisi": "Keamanan"
},
{
"id_divisi": "DI-71-0",
"nama_divisi": "Humas"
},
{
"id_divisi": "DI-71-1",
"nama_divisi": "PDD"
},
{
"id_divisi": "DI-71-2",
"nama_divisi": "Keamanan"
}
],
"timeline": [
{
"id_timeline": "TL-71-0",
"tanggal_timeline": "2017-10-21",
"deskripsi_timeline": "Pengumuman Tes Wawancara"
},
{
"id_timeline": "TL-71-0",
"tanggal_timeline": "2017-10-21",
"deskripsi_timeline": "Pengumuman Tes Wawancara"
},
{
"id_timeline": "TL-71-0",
"tanggal_timeline": "2017-10-21",
"deskripsi_timeline": "Pengumuman Tes Wawancara"
},
{
"id_timeline": "TL-71-1",
"tanggal_timeline": "2017-10-20",
"deskripsi_timeline": "Tes Wawancara"
},
{
"id_timeline": "TL-71-1",
"tanggal_timeline": "2017-10-20",
"deskripsi_timeline": "Tes Wawancara"
},
{
"id_timeline": "TL-71-1",
"tanggal_timeline": "2017-10-20",
"deskripsi_timeline": "Tes Wawancara"
}
]
},
{
"id_event": "72",
"nama_event": "I Care",
"deskripsi": "I Care",
"tanggal": "2017-10-20",
"divisi": [
{
"id_divisi": "DI-72-0",
"nama_divisi": "Keamanan"
},
{
"id_divisi": "DI-72-1",
"nama_divisi": "Humas"
},
{
"id_divisi": "DI-72-0",
"nama_divisi": "Keamanan"
},
{
"id_divisi": "DI-72-1",
"nama_divisi": "Humas"
}
],
"timeline": [
{
"id_timeline": "TL-72-0",
"tanggal_timeline": "2017-10-25",
"deskripsi_timeline": "Wawancara"
},
{
"id_timeline": "TL-72-0",
"tanggal_timeline": "2017-10-25",
"deskripsi_timeline": "Wawancara"
},
{
"id_timeline": "TL-72-1",
"tanggal_timeline": "2017-10-24",
"deskripsi_timeline": "Pengumuman"
},
{
"id_timeline": "TL-72-1",
"tanggal_timeline": "2017-10-24",
"deskripsi_timeline": "Pengumuman"
}
]
}
]
}
任何人都可以提供帮助吗?
答案 0 :(得分:3)
我认为这可能是这个错字?
for (int j=0; j<eventArray1.length(); i++){
JSONObject eventObj1 = eventArray1.getJSONObject(j);
String namaDivisi = eventObj1.getString("nama_divisi");
divisi.add(namaDivisi);
}
你正在增加i而不是j。
尝试在此处更改代码:
for (int j=0; j<eventArray1.length(); j++)