0

我已经按照谷歌课堂教程在谷歌课堂中检索教师课程列表(下面的代码,相关部分来自谷歌提供的示例 Android 代码)。

现在我想检索特定班级中的学生列表,课堂网站上唯一有用的信息提供了关于 REST 调用的信息,我真的不太了解。

用于检索教师列表的 Android 代码 Google Classroom Classes

private List<String> getDataFromApi() throws IOException {
    ListCoursesResponse response = mActivity.mService.courses().list()
        .setPageSize(10)
        .execute();
    List<Course> courses = response.getCourses();
    List<String> names = new ArrayList<String>();
    if (courses != null) {
        for (Course course : courses) {
            names.add(course.getName());
        }
    }
    return names;
}

以下是我如何修改上述代码以尝试获取班级中的学生姓名列表。然而,API 在执行此代码时会返回 NO_PERMISSION 消息,即使教师应该有权查看他们自己班级中的学生姓名。

private List<String> getDataFromApi() throws IOException {
    ListCoursesResponse response =    mActivity.mService.courses().list()
            .setPageSize(10)
            .execute();
    List<Course> courses = response.getCourses();
    List<String> names = new ArrayList<String>();
    if (courses != null) {
        names.add(courses.size()+"");

        for (Course course : courses) {

            names.add(course.getName());
            names.add(course.getId());

            ListStudentsResponse studentsResponse = mActivity.mService.courses().students().list(course.getId())
                    .setPageSize(40)
                    .execute();

            List<Student> students = studentsResponse.getStudents();

            names.add(students.size()+"");

            for (Student student : students) {

                names.add(student.toString());
            }
        }
    }
    return names;
}
4

1 回答 1

2

呜呼,看来我必须发布一个问题才能最终自己找出答案。所以,我发布的第二段代码确实有效。问题是示例应用程序中的初始 Scope 仅请求 COURSE 访问权限而不是 ROSTER 访问权限。一旦我添加了名册范围,我就可以检索学生数据。现在我只需要学习如何解析 JSON 数据。

于 2015-07-29T03:57:06.400 回答