繁体   English   中英

在 Android 中使用 google-java-api-client 获取 Google 日历活动的开始和结束时间

[英]Get Google Calendar Events Start and End Times with google-java-api-client in Android

go 如何使用 google-api-java-client 解析用户 Google 日历中事件的开始和结束时间?

从 Google 代码安装示例 android 项目后,我可以进入我的 Google 日历并解析一些信息(如所有日历、事件名称、发布时间和摘要),但我无法终生获取事件的开始和结束时间。

我对代码的理解是这样的。

在主要活动 class (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 上到处寻找帮助,我能找到的最能帮助我的是这篇文章,但我不知道如何用我的示例项目来实现它的工作。

谢谢。

您需要使用 EventFeed 并查看 EventEntry class

http://code.google.com/p/google-api-java-client/source/browse/calendar-v2-atom-oauth-sample/src/com/google/api/client/sample/calendar/v2/型号/EventEntry.java?repo=samples

返回的包含 startTime / endTime 的 Atom 字符串将如下所示:

<gd:when startTime='2010-03-13T14:00Z' endTime='2010-03-13T14:30Z'/>

它在 EventEntry class 中建模,如下所示:

@Key("gd:when")
public When when;

(When object 的属性,使用 @Key 注释映射)

When object,模型上的 start/endTime 属性 When object

@Key("@startTime")
public DateTime startTime;

@Key("@endTime")
public DateTime endTime;

与 eventFeed 交互时的客户端代码如下所示:

EventEntry event = eventFeed.get(0);
DateTime start = event.when.startDate;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM