📄 eventfeeddemo.java
字号:
/* Copyright (c) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package sample.calendar;import com.google.gdata.client.Query;import com.google.gdata.client.calendar.CalendarQuery;import com.google.gdata.client.calendar.CalendarService;import com.google.gdata.data.DateTime;import com.google.gdata.data.PlainTextConstruct;import com.google.gdata.data.calendar.CalendarEntry;import com.google.gdata.data.calendar.CalendarEventEntry;import com.google.gdata.data.calendar.CalendarEventFeed;import com.google.gdata.data.calendar.CalendarFeed;import com.google.gdata.data.calendar.WebContent;import com.google.gdata.data.extensions.ExtendedProperty;import com.google.gdata.data.extensions.Recurrence;import com.google.gdata.data.extensions.Reminder;import com.google.gdata.data.extensions.When;import com.google.gdata.util.ServiceException;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.TimeZone;/** * Demonstrates basic Calendar Data API operations on the event feed using the * Java client library: * * <ul> * <li>Retrieving the list of all the user's calendars</li> * <li>Retrieving all events on a single calendar</li> * <li>Performing a full-text query on a calendar</li> * <li>Performing a date-range query on a calendar</li> * <li>Creating a single-occurrence event</li> * <li>Creating a recurring event</li> * <li>Creating a quick add event</li> * <li>Creating a web content event</li> * <li>Updating events</li> * <li>Adding reminders and extended properties</li> * <li>Deleting events</li> * </ul> */public class EventFeedDemo { // The base URL for a user's calendar metafeed (needs a username appended). private static final String METAFEED_URL_BASE = "http://www.google.com/calendar/feeds/"; // The string to add to the user's metafeedUrl to access the event feed for // their primary calendar. private static final String EVENT_FEED_URL_SUFFIX = "/private/full"; // The URL for the metafeed of the specified user. // (e.g. http://www.google.com/feeds/calendar/jdoe@gmail.com) private static URL metafeedUrl = null; // The URL for the event feed of the specified user's primary calendar. // (e.g. http://www.googe.com/feeds/calendar/jdoe@gmail.com/private/full) private static URL eventFeedUrl = null; /** * Prints a list of all the user's calendars. * * @param service An authenticated CalendarService object. * @throws ServiceException If the service is unable to handle the request. * @throws IOException Error communicating with the server */ private static void printUserCalendars(CalendarService service) throws IOException, ServiceException { // Send the request and receive the response: CalendarFeed resultFeed = service.getFeed(metafeedUrl, 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()); } System.out.println(); } /** * Prints the titles of all events on the calendar specified by * {@code feedUri}. * * @param service An authenticated CalendarService object. * @throws ServiceException If the service is unable to handle the request. * @throws IOException Error communicating with the server. */ private static void printAllEvents(CalendarService service) throws ServiceException, IOException { // Send the request and receive the response: CalendarEventFeed resultFeed = service.getFeed(eventFeedUrl, CalendarEventFeed.class); System.out.println("All events on your calendar:"); System.out.println(); for (int i = 0; i < resultFeed.getEntries().size(); i++) { CalendarEventEntry entry = resultFeed.getEntries().get(i); System.out.println("\t" + entry.getTitle().getPlainText()); } System.out.println(); } /** * Prints the titles of all events matching a full-text query. * * @param service An authenticated CalendarService object. * @param query The text for which to query. * @throws ServiceException If the service is unable to handle the request. * @throws IOException Error communicating with the server. */ private static void fullTextQuery(CalendarService service, String query) throws ServiceException, IOException { Query myQuery = new Query(eventFeedUrl); myQuery.setFullTextQuery("Tennis"); CalendarEventFeed resultFeed = service.query(myQuery, CalendarEventFeed.class); System.out.println("Events matching " + query + ":"); System.out.println(); for (int i = 0; i < resultFeed.getEntries().size(); i++) { CalendarEventEntry entry = resultFeed.getEntries().get(i); System.out.println("\t" + entry.getTitle().getPlainText()); } System.out.println(); } /** * Prints the titles of all events in a specified date/time range. * * @param service An authenticated CalendarService object. * @param startTime Start time (inclusive) of events to print. * @param endTime End time (exclusive) of events to print. * @throws ServiceException If the service is unable to handle the request. * @throws IOException Error communicating with the server. */ private static void dateRangeQuery(CalendarService service, DateTime startTime, DateTime endTime) throws ServiceException, IOException { CalendarQuery myQuery = new CalendarQuery(eventFeedUrl); myQuery.setMinimumStartTime(startTime); myQuery.setMaximumStartTime(endTime); // Send the request and receive the response: CalendarEventFeed resultFeed = service.query(myQuery, CalendarEventFeed.class); System.out.println("Events from " + startTime.toString() + " to " + endTime.toString() + ":"); System.out.println(); for (int i = 0; i < resultFeed.getEntries().size(); i++) { CalendarEventEntry entry = resultFeed.getEntries().get(i); System.out.println("\t" + entry.getTitle().getPlainText()); } System.out.println(); } /** * Helper method to create either single-instance or recurring events. For * simplicity, some values that might normally be passed as parameters (such * as author name, email, etc.) are hard-coded. * * @param service An authenticated CalendarService object. * @param eventTitle Title of the event to create. * @param eventContent Text content of the event to create. * @param recurData Recurrence value for the event, or null for * single-instance events. * @param isQuickAdd True if eventContent should be interpreted as the text of * a quick add event. * @param wc A WebContent object, or null if this is not a web content event. * @return The newly-created CalendarEventEntry. * @throws ServiceException If the service is unable to handle the request. * @throws IOException Error communicating with the server. */ private static CalendarEventEntry createEvent(CalendarService service, String eventTitle, String eventContent, String recurData, boolean isQuickAdd, WebContent wc) throws ServiceException, IOException { CalendarEventEntry myEntry = new CalendarEventEntry(); myEntry.setTitle(new PlainTextConstruct(eventTitle)); myEntry.setContent(new PlainTextConstruct(eventContent)); myEntry.setQuickAdd(isQuickAdd); myEntry.setWebContent(wc); // If a recurrence was requested, add it. Otherwise, set the // time (the current date and time) and duration (30 minutes) // of the event. if (recurData == null) { Calendar calendar = new GregorianCalendar(); DateTime startTime = new DateTime(calendar.getTime(), TimeZone .getDefault()); calendar.add(Calendar.MINUTE, 30); DateTime endTime = new DateTime(calendar.getTime(), TimeZone.getDefault()); When eventTimes = new When(); eventTimes.setStartTime(startTime); eventTimes.setEndTime(endTime); myEntry.addTime(eventTimes); } else { Recurrence recur = new Recurrence(); recur.setValue(recurData); myEntry.setRecurrence(recur); } // Send the request and receive the response: return service.insert(eventFeedUrl, myEntry); } /** * Creates a single-occurrence event. * * @param service An authenticated CalendarService object. * @param eventTitle Title of the event to create. * @param eventContent Text content of the event to create. * @return The newly-created CalendarEventEntry. * @throws ServiceException If the service is unable to handle the request. * @throws IOException Error communicating with the server. */ private static CalendarEventEntry createSingleEvent(CalendarService service, String eventTitle, String eventContent) throws ServiceException, IOException { return createEvent(service, eventTitle, eventContent, null, false, null);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -