【问题标题】:How to retrieve a list of students in a teacher's class in Google Classroom for Android App?如何在 Google Classroom for Android App 中检索教师班级的学生列表?
【发布时间】:2015-07-29 01:15:08
【问题描述】:

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

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

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

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;
}

【问题讨论】:

  • 您的问题不清楚,您想在您的应用程序中使用 Google Class room API 还是您正在开发一个示例应用程序,您正在创建师生管理系统?
  • 我已经有一个供教师使用的 Android 应用程序,我想使用 Classroom API 来获取课程列表以及每门课程中的学生姓名。 Classroom 团队发布了一些示例代码,展示了如何验证用户身份并获取用户 ID 下的课程列表。它工作得很好。现在我想知道如何获取特定 classid 下的学生姓名列表 - 这一切都适用于 Android 应用程序。
  • 如果有任何可用的 API,那么它应该可以工作,您使用的是哪个 API?在这里发帖。
  • 我让它的工作方式与您的代码类似。通过 OAuth2 授权时,我确实必须将 ClassroomScopes.CLASSROOM_ROSTERS_READONLY 添加到范围。如果您从快速入门开始工作,请使用 MainActivity - private static final String[] SCOPES = { ClassroomScopes.CLASSROOM_ROSTERS_READONLY, ClassroomScopes.CLASSROOM_PROFILE_EMAILS };

标签: android google-api google-classroom


【解决方案1】:

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

【讨论】:

  • 如果您使用该库,就像看起来一样,则无需解析 JSON。
  • 我找不到从特定课程 ID 中获取学生姓名列表的方法。我用 List students = studentsResponse.getStudents();然后 student.toString() 我找不到代码来获取学生的名字和姓氏。用户 ID 或配置文件只有一个选项,因此 toString() 以 JSON 格式给了我整个学生对象,然后我确实设法解析它以获取名字和姓氏以及电子邮件。如果可能的话,我想直接得到它。有什么建议吗?
  • student.getProfile().getName().getFullName(); student.getProfile().getEmailAddress();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多