Java使用Google Calendar


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/[email protected]/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的紅色部分([email protected]) 必須替換爲 創建 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的操作.


發佈了57 篇原創文章 · 獲贊 102 · 訪問量 59萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章