如何使用 google-api-java-client 解析用户 Google 日历中事件的开始和结束时间?
从 Google 代码安装此示例 android 项目后,我可以进入我的 Google 日历并解析一些信息(如所有日历、事件名称、发布时间和摘要),但我无法终生获取事件的开始和结束时间。
我对代码的理解是这样的。
在主活动类 (CalendarAndroidSample.java) 中,这是获取每个日历标题的方法:
void executeRefreshCalendars() {
String[] calendarNames;
List<CalendarEntry> calendars = this.calendars;
calendars.clear();
try {
CalendarUrl url = CalendarUrl.forAllCalendarsFeed();
// page through results
while (true) {
CalendarFeed feed = client.executeGetCalendarFeed(url);
if (feed.calendars != null) {
calendars.addAll(feed.calendars);
}
String nextLink = feed.getNextLink();
if (nextLink == null) {
break;
}
}
int numCalendars = calendars.size();
calendarNames = new String[numCalendars];
for (int i = 0; i < numCalendars; i++) {
calendarNames[i] = calendars.get(i).title;
}
} catch (IOException e) {
handleException(e);
calendarNames = new String[] {e.getMessage()};
calendars.clear();
}
上面的 for 循环将我帐户中每个日历的标题分配给字符串数组“calendarNames[]”。
我发现在此处找到的项目 (Entry.java) 内的一个单独的 java 文件中,@Key 注释指示代码解析 XML 元素,并且字符串的名称应该与元素的名称匹配。
public class Entry implements Cloneable {
@Key
public String summary;
@Key
public String title;
@Key
public String updated;
@Key
public String published;
@Key("link")
public List<Link> links;
@Override
protected Entry clone() {
try {
@SuppressWarnings("unchecked")
Entry result = (Entry) super.clone();
Data.deepCopy(this, result);
return result;
} catch (CloneNotSupportedException e) {
throw new IllegalStateException(e);
}
}
String getEditLink() {
return Link.find(links, "edit");
}
}
所以....
@Key
public String published;
...将在名为“已发布”的 XML 中找到元素并将该元素的值分配给字符串。
因此,返回到第一个引用的 java 方法 executeRefreshCalendars()(在 CalendarAndroidSample.java 中),改变
calendarNames[i] = calendars.get(i).title;
到
calendarNames[i] = calendars.get(i).published;
给我事件发布的日期。
我认为我在理解这段代码方面的问题是,对于事件的开始和结束时间,数据位于一个包含两部分的 XML 元素中。
谁能帮助我了解如何做到这一点?我在浏览器中打开了 10 多个选项卡,并在 SO 上到处寻找帮助,我能找到的最能帮助我的是这篇文章,但我不知道如何用我的示例项目来实现它正在与。
谢谢。