简体   繁体   English

如何使用Java从Google日历获取事件?

[英]How to get event from Google Calendar in Java?

I use this code for getting Google calendars list: 我使用以下代码获取Google日历列表:

// Create a CalenderService and authenticate
CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("j...@gmail.com", "mypassword");

// Send the request and print the response
URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/allcalendars/full");
CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
System.out.println("Your calendars:");
System.out.println();
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
  CalendarEntry entry = resultFeed.getEntries().get(i);
  System.out.println("\t" + entry.getTitle().getPlainText());
}

This code return me calendars list, but I need to get all events from this calendar about today. 这段代码返回了我的日历列表,但是我需要从该日历中获取今天的所有事件。 How can I do that? 我怎样才能做到这一点?

If you are using the sample program to start with please use ..../private/full to get the events from Calendar. 如果您使用示例程序作为开始,请使用..../private/full从日历中获取事件。

For example: 例如:

"https://www.google.com/calendar/feeds/xxx@gmail.com/private/full");

You can also use a this default URL, instead of specifying xxx@gmail.com, 您也可以使用此默认网址,而不是指定xxx@gmail.com,

"https://www.google.com/calendar/feeds/default/private/full"

And I think you should use CalendarEventFeed Object instead of CalendarFeed See this code snippet below: 而且我认为您应该使用CalendarEventFeed对象而不是CalendarFeed参见下面的代码片段:

CalendarQuery myQuery = new CalendarQuery(feedUrl);
myService.setUserCredentials(p.getgUser(), p.getgPassword());

 DateTime dt = new DateTime();
 myQuery.setMinimumStartTime(DateTime.now());
 myQuery.setMaximumStartTime(dt);

CalendarEventFeed resultFeed = myService.query(myQuery, CalendarEventFeed.class);

Then traverse the contents of resultFeed 然后遍历resultFeed的内容

..... .....

CalendarEventFeed resultFeed = calendarData

for (int i = 0; i<calendarData.getEntries().size(); i++) {
          CalendarEventEntry entry = calendarData.getEntries().get(i);

..... .....

Assuming you're using the official GData API: 假设您使用的是官方的GData API:

Try using the getFeed(Query query, …) overload with a CalendarQuery - that seems to let you specify the range for the start time of an event. 尝试使用getFeed(Query query, …)过载与CalendarQuery -这似乎让你指定范围的事件的开始时间。

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

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