2017-11-11 46 views
0

我正在按照上面的錯誤是我的代碼和日誌貓安卓:無法採取API響應到ArrayList中

public class TimeMain { 
String name; 

public ArrayList<Lecture> getLectures() { 
    return lectures; 
} 

public void setLectures(ArrayList<Lecture> lectures) { 
    this.lectures = lectures; 
} 

ArrayList<Lecture> lectures; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 
} 


class Lecture { 
String timming,subject,teachers,rooms; 

public String getTimming() { 
    return timming; 
} 

public void setTimming(String timming) { 
    this.timming = timming; 
} 

public String getSubject() { 
    return subject; 
} 

public void setSubject(String subject) { 
    this.subject = subject; 
} 

public String getTeachers() { 
    return teachers; 
} 

public void setTeachers(String teachers) { 
    this.teachers = teachers; 
} 

public String getRooms() { 
    return rooms; 
} 

public void setRooms(String rooms) { 
    this.rooms = rooms; 
} 
} 

這是我的API響應

[ 
{ 
    "name": "Monday", 
    "lectures": [ 
     { 
      "timing": "9:00-10:00", 
      "subject": "TOC", 
      "teachers": "MK", 
      "rooms": "MK" 
     }, 
     { 
      "timing": "10:00-11:00", 
      "subject": "TOC", 
      "teachers": "MK", 
      "rooms": "MK" 
     }, 
     { 
      "timing": "11:00-12:00", 
      "subject": "JAVA", 
      "teachers": "SSJ", 
      "rooms": "SSJ" 
     }, 
     { 
      "timing": "12:00-12:30", 
      "subject": "break", 
      "teachers": null, 
      "rooms": null 
     }, 
     { 
      "timing": "12:30-1:30", 
      "subject": "CG", 
      "teachers": "VJ", 
      "rooms": "VJ" 
     }, 
     { 
      "timing": "1:30-2:30", 
      "subject": "CG", 
      "teachers": "VJ", 
      "rooms": "VJ" 
     } 
    ] 
}, 
{ 
    "name": "Tuesday", 
    "lectures": [ 
     { 
      "timing": "9:00-10:00", 
      "subject": "DCN", 
      "teachers": "VJ", 
      "rooms": "VJ" 
     }, 
     { 
      "timing": "10:00-11:00", 
      "subject": "C#", 
      "teachers": "US", 
      "rooms": "US" 
     }, 
     { 
      "timing": "11:00-12:00", 
      "subject": "JAVA", 
      "teachers": "SSJ", 
      "rooms": "SSJ" 
     }, 
     { 
      "timing": "12:00-12:30", 
      "subject": "break", 
      "teachers": null, 
      "rooms": null 
     }, 
     { 
      "timing": "12:30-1:30", 
      "subject": "TOC", 
      "teachers": "MK", 
      "rooms": "MK" 
     }, 
     { 
      "timing": "1:30-2:30", 
      "subject": "TOC", 
      "teachers": "MK", 
      "rooms": "MK" 
     } 
    ] 
}, 
{ 
    "name": "Wednesday", 
    "lectures": [ 
     { 
      "timing": "9:00-10:00", 
      "subject": "C#", 
      "teachers": "US", 
      "rooms": "US" 
     }, 
     { 
      "timing": "10:00-11:00", 
      "subject": "DCN", 
      "teachers": "VJ", 
      "rooms": "VJ" 
     }, 
     { 
      "timing": "11:00-12:00", 
      "subject": "CG", 
      "teachers": "VJ", 
      "rooms": "VJ" 
     }, 
     { 
      "timing": "12:00-12:30", 
      "subject": "break", 
      "teachers": null, 
      "rooms": null 
     }, 
     { 
      "timing": "12:30-1:30", 
      "subject": "TOC", 
      "teachers": "MK", 
      "rooms": "MK" 
     }, 
     { 
      "timing": "1:30-2:30", 
      "subject": "JAVA", 
      "teachers": "SSJ", 
      "rooms": "SSJ" 
     } 
    ] 
}, 
{ 
    "name": "Thursday", 
    "lectures": [ 
     { 
      "timing": "9:00-10:00", 
      "subject": "TOC", 
      "teachers": "MK", 
      "rooms": "MK" 
     }, 
     { 
      "timing": "10:00-11:00", 
      "subject": "TOC", 
      "teachers": "MK", 
      "rooms": "MK" 
     }, 
     { 
      "timing": "11:00-12:00", 
      "subject": "JAVA", 
      "teachers": "SSJ", 
      "rooms": "SSJ" 
     }, 
     { 
      "timing": "12:00-12:30", 
      "subject": "break", 
      "teachers": null, 
      "rooms": null 
     }, 
     { 
      "timing": "12:30-1:30", 
      "subject": "CG", 
      "teachers": "VJ", 
      "rooms": "VJ" 
     }, 
     { 
      "timing": "1:30-2:30", 
      "subject": "CG", 
      "teachers": "VJ", 
      "rooms": "VJ" 
     } 
    ] 
}, 
{ 
    "name": "Friday", 
    "lectures": [ 
     { 
      "timing": "9:00-10:00", 
      "subject": "DCN", 
      "teachers": "VJ", 
      "rooms": "VJ" 
     }, 
     { 
      "timing": "10:00-11:00", 
      "subject": "C#", 
      "teachers": "US", 
      "rooms": "US" 
     }, 
     { 
      "timing": "11:00-12:00", 
      "subject": "JAVA", 
      "teachers": "SSJ", 
      "rooms": "SSJ" 
     }, 
     { 
      "timing": "12:00-12:30", 
      "subject": "break", 
      "teachers": null, 
      "rooms": null 
     }, 
     { 
      "timing": "12:30-1:30", 
      "subject": "TOC", 
      "teachers": "MK", 
      "rooms": "MK" 
     }, 
     { 
      "timing": "1:30-2:30", 
      "subject": "TOC", 
      "teachers": "MK", 
      "rooms": "MK" 
     } 
    ] 
}, 
{ 
    "name": "Saturday", 
    "lectures": [ 
     { 
      "timing": "9:00-10:00", 
      "subject": "C#", 
      "teachers": "US", 
      "rooms": "US" 
     }, 
     { 
      "timing": "10:00-11:00", 
      "subject": "DCN", 
      "teachers": "VJ", 
      "rooms": "VJ" 
     }, 
     { 
      "timing": "11:00-12:00", 
      "subject": "CG", 
      "teachers": "VJ", 
      "rooms": "VJ" 
     }, 
     { 
      "timing": "12:00-12:30", 
      "subject": "break", 
      "teachers": null, 
      "rooms": null 
     }, 
     { 
      "timing": "12:30-1:30", 
      "subject": "TOC", 
      "teachers": "MK", 
      "rooms": "MK" 
     }, 
     { 
      "timing": "1:30-2:30", 
      "subject": "JAVA", 
      "teachers": "SSJ", 
      "rooms": "SSJ" 
     } 
    ] 
}, 
{ 
    "name": "Sunday", 
    "lectures": [ 
     { 
      "timing": "12:00-12:30", 
      "subject": "break", 
      "teachers": null, 
      "rooms": null 
     } 
    ] 
} 
] 

登錄貓

11-11 18:06:39.849 5858-5858/com.matrixdev.bvicam.bvicam W/System.err:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:預期的BEGIN_OBJECT,但BEGIN_ARRAY在第1行第2列路徑$

我想利用上述響應到數組列表,該代碼我使用

TimeResponse timeResponse = (TimeResponse) responseObj; 
     nameList = new ArrayList<>(); 

     for (TimeMain timeMain : timeResponse.getTimeMains()) { 
      nameList.add(timeMain.getName()); 
     } 

TimeResponse類

public class TimeResponse { 
ArrayList<TimeMain> timeMains; 

public ArrayList<TimeMain> getTimeMains() { 
    return timeMains; 
} 

public void setTimeMains(ArrayList<TimeMain> timeMains) { 
    this.timeMains = timeMains; 
} 
} 
+0

請你精確你在哪個行號得到錯誤? –

+0

製作其中一個[MCVE]請 – LW001

+0

您使用的是api調用和gson轉換器的庫? – sabbibJAVA

回答

0
List<TimeMain> timeList = new Gson().fromJson(json, new TypeToken<List<TimeMain>>(){}.getTypeToken()); 

應該做的工作