2011-06-09 23 views
4

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

从Google代码安装this示例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文件中发现here,@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个选项卡,并且无处不在寻求帮助,最近的帮助是this,但我无法弄清楚如何使用示例项目实现它正在合作。

谢谢。

回答

3

你需要使用EventFeed,并采取看看EventEntry类

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

凌动返回的字符串包含的开始时间/结束看起来就像这样:

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

它仿照在EventEntry类是这样的:

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

的对象时,模型与eventFeed inteacting当在当对象

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

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

客户端代码的开始/结束时间属性(用于当对象的属性,使用@key注解映射)将看起来像这样:

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

这是一个很好的回应。现在正在工作。 – dell116 2011-06-10 18:39:25

+0

有什么机会可以帮助我解决有关此API的另一个问题?如果您有免费的第二个或第二个,请参阅http://stackoverflow.com/questions/6319361/set-google-calendar-query-parameters-with-google-java-api-client-in-android。 – dell116 2011-06-12 17:47:42

+0

看看我的答案。我认为这是使用google-api-java-client创建网址的最简单方法。 – ddewaele 2011-06-12 22:59:49

相关问题