如何将一去使用谷歌-API的Java客户端解析事件的开始和结束时间在用户的谷歌日历?
How would one go about parsing the start and end times of events in a users Google Calendar using the google-api-java-client?
在安装这个的示例Android项目由谷歌code,我可以进入我的谷歌日历和分析的一些信息,(像所有的日历,事件名字,当它被公布,并且摘要),但我不能为我的生命得到事件开始和结束时间。
After installing this sample android project from Google code, I can get into my Google calendar and parse some information, (like all the calendars, the event names, when it was published, and the summary), but I cannot for the life of me get the event start and end times.
我的code的理解是这样。
My understanding of the code is as such.
里面的主要活动类(CalendarAndroidSample.java)这是得到的称号,我的每个日历的方法:
Inside the main activity class (CalendarAndroidSample.java) this is the method that gets the title for each of my calendars:
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 []。
The for-loop above is assigning the title of each calendar in my account to the string array "calendarNames[]."
我已经想通了,在项目内(Entry.java)内的separete的java文件中找到此处的@key注释指示code解析XML元素和字符串的名称应与该元素的名称。
I have figured out that within a separete java file inside the project (Entry.java) found here, the @Key annotation directs the code to parse an XML element and the name of the string should match the name of the element.
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");
}
}
所以......
So....
@Key
public String published;
...会发现在一个名为发布的XML元素和元素的值分配给字符串。
...would find the element in the XML named "published" and assign the value of that element to the string.
因此,返回到第一引用的Java方法executeRefreshCalendars()(内CalendarAndroidSample.java),改变
Thus, returning to the first referenced java method executeRefreshCalendars() (inside CalendarAndroidSample.java), changing
calendarNames[i] = calendars.get(i).title;
到
calendarNames[i] = calendars.get(i).published;
给我的事件的发布日期。
gives me the date the event was published.
我觉得我在考虑的问题,以了解该code是一个事件的开始和结束时间,数据就在于有两个部分的XML元素中。
I think my problem in regard to understanding this code is that for an event's start and end time, the data lies within an XML element that has two parts.
谁能帮我获得关于如何做到这一点的一些见解?我有超过10个标签在我的浏览器打开,并到处找对苏的帮助就这个问题和最接近我能找到帮助我是this职位,但我无法弄清楚如何使用示例项目我正在与执行。
Can anyone help me gain some insight on how to do this? I have more than 10 tabs open in my browsers and have looked everywhere on SO for help on this and the closest thing I can find to helping me is this post, but I can't figure out how to implement it with the sample project I'm working with.
感谢。
您需要使用EventFeed,并采取一看EventEntry类
You'll need to use the EventFeed and take a look at the EventEntry class
http://$c$c.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
凌动字符串返回一个包含了开始时间/结束将是这样的:
The Atom string returned containing the startTime / endTime will look like this :
<gd:when startTime='2010-03-13T14:00Z' endTime='2010-03-13T14:30Z'/>
它为蓝本的EventEntry类是这样的:
It's modelled in the EventEntry class like this :
@Key("gd:when")
public When when;
(一为当对象的属性,映射使用@key注释)
(a property for the When object, mapped using the @Key annotation)
的对象时,模型的开始时间/结束的当对象的属性。
The When object, models the start/endTime attributes on the When object
@Key("@startTime")
public DateTime startTime;
@Key("@endTime")
public DateTime endTime;
客户端code与eventFeed inteacting看起来像这样的时候:
Client code when inteacting with the eventFeed will look like this :
EventEntry event = eventFeed.get(0);
DateTime start = event.when.startDate;