Google Calendar, 一個不錯的東西, 用她可以方便地進行議程管理,組織,分享等, 甚至還包括寄件提醒, 手機簡訊提醒 等.
參考Google Calender JAVA API(V2): https://developers.google.com/google-apps/calendar/v2/developers_guide_java
下面開始 使用JAVA 調用 Google Calender, 主要包括 Event add/edit/delete, Calendar add/delete/list
1.添加 gdata 的依賴:
在此提供兩種方式添加依賴, 使用MAVEN與直接使用jar檔案.
1.1 使用Maven
在POM檔案中添加以下依賴:
<dependency> <groupId>org.openengsb.wrapped</groupId> <artifactId>com.google.gdata-calendar</artifactId> <version>1.41.5.w1</version> </dependency>
1.2 使用jar檔案
如果不使用MAVEN, 則到http://code.google.com/p/gdata-java-client/downloads/list 直接去下載 相應的jar檔案, 加入到 classpath中即可.
2.主要代碼:
>>> 擷取 CalendarService
CalendarService 是API的核心類, 所有操作都是調用該類的具體方法實現, 如getFeed,insert等,
建立該類的執行個體必須 指定 GMAIL 使用者名稱,密碼. 代碼如下:
private static CalendarService createCalendarService(String gmailName, String gmailPass) throws AuthenticationException { CalendarService calendarService = new CalendarService("myCalendarService"); calendarService.setUserCredentials(gmailName, gmailPass); return calendarService; }
注: myCalendarService 只是一個名字,沒實際含義
2.1 Event add/edit/delete
Event, 具體指一個Calendar 裡面的一個 活動,
* add event
CalendarEventEntry myEntry = new CalendarEventEntry(); long millis = System.currentTimeMillis(); myEntry.setTitle(new PlainTextConstruct("A new Event Entry > " + millis)); myEntry.setContent(new PlainTextConstruct("I'm content")); TimeZone timeZone = TimeZone.getDefault(); DateTime startTime = new DateTime(new Date(), timeZone); DateTime endTime = new DateTime(new Date(millis + 10000000), timeZone); When eventTimes = new When(); eventTimes.setStartTime(startTime); eventTimes.setEndTime(endTime); myEntry.addTime(eventTimes); URL postUrl = new URL("https://www.google.com/calendar/feeds/my.test@gmail.com/private/full"); CalendarEventEntry eventEntry = calendarService.insert(postUrl, myEntry); String href = eventEntry.getEditLink().getHref(); System.out.println("Edit URL: " + href);
說明:
> Event對應的實體名叫 CalendarEventEntry. 在使用時必須 建立一個執行個體.
> Call setTitle, setContent 設定 Event的標題與內容.
> When類型用於設定 Event的開始時間(startTime)與結束時間(endTime), 在使用時最好指定TimeZone.
> 注意 URL的紅色部分(my.test@gmail.com) 必須替換為 建立 CalendarService的 gmail 帳戶名稱.
> 調用 CalendarService的 insert 方法即可 add 一個新的Event.
> 注意 在添加完成後的那行代碼
String href = eventEntry.getEditLink().getHref();
這兒的 href 變數為 添加的Event的串連地址, 需要儲存,用於在編輯或刪除時從Google Calendar擷取 CalendarEventEntry 執行個體.一個樣本:
https://www.google.com/calendar/feeds/my.test%40gmail.com/private/full/9dfcab6hsqb7ld4ppc9quuch88
添加後的 Google Calendar 頁面如下:
* edit/delete event
對於edit/delete 操作的第一步是如何擷取對應 的 CalendarEventEntry 執行個體. 具體步驟為:
1). 用gmail建立建立 CalendarService 執行個體
2). 通過添加 Event 擷取的 串連地址(href) 調用CalendarService.getEntry 擷取對應的CalendarEventEntry 執行個體.
3). 對CalendarEventEntry進行相應的edit,delete.
edit code:
//get URL getUrl = new URL(href); CalendarEventEntry entry = calendarService.getEntry(getUrl, CalendarEventEntry.class); //edit entry.setTitle(new PlainTextConstruct("I'm a new title")); URL editUrl = new URL(entry.getEditLink().getHref()); CalendarEventEntry updatedEntry = calendarService.update(editUrl, entry);
delete code:
//get URL getUrl = new URL(href); CalendarEventEntry entry = calendarService.getEntry(getUrl, CalendarEventEntry.class); //del entry.delete();
2.2 Calendar add/delete/list
在Google Calendar 中,預設有一個以 郵箱名命名的日曆, 該日曆可以擷取,修改,但不能刪除(其他添加的日曆是可以刪除的).
* add calendar
CalendarEntry entry = new CalendarEntry(); entry.setTitle(new PlainTextConstruct("A new Calendar")); entry.setSummary(new PlainTextConstruct("I have a new Calendar.")); entry.setTimeZone(new TimeZoneProperty("Asia/Shanghai")); entry.setHidden(HiddenProperty.FALSE); entry.setColor(new ColorProperty("#2952A3")); entry.addLocation(new Where("", "", "Shanghai")); URL postUrl = new URL("https://www.google.com/entry/feeds/default/owncalendars/full"); CalendarEntry returnedCalendar = calendarService.insert(postUrl, entry); System.out.println("ID =" + returnedCalendar.getId());
* edit/delete calendar (這段代碼參考 API中的樣本)
delete
URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/owncalendars/full"); CalendarFeed resultFeed = calendarService.getFeed(feedUrl, CalendarFeed.class); List<CalendarEntry> entries = resultFeed.getEntries(); for (CalendarEntry entry : entries) { System.out.println("Delete > " + entry.getId() + ", " + entry.getTitle().getPlainText()); try { entry.delete(); } catch (InvalidEntryException e) { e.printStackTrace(); } }
edit
URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/owncalendars/full"); CalendarFeed resultFeed = calendarService.getFeed(feedUrl, CalendarFeed.class); List<CalendarEntry> entries = resultFeed.getEntries(); for (CalendarEntry entry : entries) { entry.setColor(new ColorProperty("#2952A3")); String href = entry.getEditLink().getHref(); URL edUrl = new URL(href); calendarService.update(edUrl, entry); }
在真實的使用環境中.對Calendar的操作一般比較少, 更多 的是對 Event的操作.