📄 eventfeeddemo.java
字号:
} /** * Creates a quick add event. * * @param service An authenticated CalendarService object. * @param quickAddContent The quick add text, including the event title, date * and time. * @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 createQuickAddEvent( CalendarService service, String quickAddContent) throws ServiceException, IOException { return createEvent(service, null, quickAddContent, null, true, null); } /** * Creates a web content event. * * @param service An authenticated CalendarService object. * @param title The title of the web content event. * @param type The MIME type of the web content event, e.g. "image/gif" * @param url The URL of the content to display in the web content window. * @param icon The icon to display in the main Calendar user interface. * @param width The width of the web content window. * @param height The height of the web content window. * @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 createWebContentEvent( CalendarService service, String title, String type, String url, String icon, String width, String height) throws ServiceException, IOException { WebContent wc = new WebContent(); wc.setHeight(height); wc.setWidth(width); wc.setTitle(title); wc.setType(type); wc.setUrl(url); wc.setIcon(icon); return createEvent(service, title, null, null, false, wc); } /** * Creates a new recurring 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 createRecurringEvent( CalendarService service, String eventTitle, String eventContent) throws ServiceException, IOException { // Specify a recurring event that occurs every Tuesday from May 1, // 2007 through September 4, 2007. Note that we are using iCal (RFC 2445) // syntax; see http://www.ietf.org/rfc/rfc2445.txt for more information. String recurData = "DTSTART;VALUE=DATE:20070501\r\n" + "DTEND;VALUE=DATE:20070502\r\n" + "RRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20070904\r\n"; return createEvent(service, eventTitle, eventContent, recurData, false, null); } /** * Updates the title of an existing calendar event. * * @param entry The event to update. * @param newTitle The new title for this event. * @return The updated CalendarEventEntry object. * @throws ServiceException If the service is unable to handle the request. * @throws IOException Error communicating with the server. */ private static CalendarEventEntry updateTitle(CalendarEventEntry entry, String newTitle) throws ServiceException, IOException { entry.setTitle(new PlainTextConstruct(newTitle)); return entry.update(); } /** * Adds a reminder to a calendar event. * * @param entry The event to update. * @param numMinutes Reminder time, in minutes. * @return The updated EventEntry object. * @throws ServiceException If the service is unable to handle the request. * @throws IOException Error communicating with the server. */ private static CalendarEventEntry addReminder(CalendarEventEntry entry, int numMinutes) throws ServiceException, IOException { Reminder reminder = new Reminder(); reminder.setMinutes(numMinutes); entry.getReminder().add(reminder); return entry.update(); } /** * Adds an extended property to a calendar event. * * @param entry The event to update. * @return The updated EventEntry object. * @throws ServiceException If the service is unable to handle the request. * @throws IOException Error communicating with the server. */ private static CalendarEventEntry addExtendedProperty(CalendarEventEntry entry) throws ServiceException, IOException { // Add an extended property "id" with value 1234 to the EventEntry entry. // We specify the complete schema URL to avoid namespace collisions with // other applications that use the same property name. ExtendedProperty property = new ExtendedProperty(); property.setName("http://www.example.com/schemas/2005#mycal.id"); property.setValue("1234"); entry.addExtension(property); return entry.update(); } /** * Instantiates a CalendarService object and uses the command line arguments * to authenticate. The CalendarService object is used to demonstrate * interactions with the Calendar data API's event feed. * * @param args Must be length 2 and contain a valid username/password */ public static void main(String[] args) { CalendarService myService = new CalendarService("exampleCo-exampleApp-1"); // Set username and password from command-line arguments. if (args.length != 2) { usage(); return; } String userName = args[0]; String userPassword = args[1]; // Create the necessary URL objects. try { metafeedUrl = new URL(METAFEED_URL_BASE + userName); eventFeedUrl = new URL(METAFEED_URL_BASE + userName + EVENT_FEED_URL_SUFFIX); } catch (MalformedURLException e) { // Bad URL System.err.println("Uh oh - you've got an invalid URL."); e.printStackTrace(); return; } try { myService.setUserCredentials(userName, userPassword); // Demonstrate retrieving a list of the user's calendars. printUserCalendars(myService); // Demonstrate various feed queries. System.out.println("Printing all events"); printAllEvents(myService); System.out.println("Full text query"); fullTextQuery(myService, "Tennis"); dateRangeQuery(myService, DateTime.parseDate("2007-01-05"), DateTime .parseDate("2007-01-07")); // Demonstrate creating a single-occurrence event. CalendarEventEntry singleEvent = createSingleEvent(myService, "Tennis with Mike", "Meet for a quick lesson."); System.out.println("Successfully created event " + singleEvent.getTitle().getPlainText()); // Demonstrate creating a quick add event. CalendarEventEntry quickAddEvent = createQuickAddEvent(myService, "Tennis with John April 11 3pm-3:30pm"); System.out.println("Successfully created quick add event " + quickAddEvent.getTitle().getPlainText()); // Demonstrate creating a web content event. CalendarEventEntry webContentEvent = createWebContentEvent(myService, "World Cup", "image/gif", "http://www.google.com/logos/worldcup06.gif", "http://www.google.com/calendar/images/google-holiday.gif", "276", "120"); System.out.println("Successfully created web content event " + webContentEvent.getTitle().getPlainText()); // Demonstrate creating a recurring event. CalendarEventEntry recurringEvent = createRecurringEvent(myService, "Tennis with Dan", "Weekly tennis lesson."); System.out.println("Successfully created recurring event " + recurringEvent.getTitle().getPlainText()); // Demonstrate updating the event's text. singleEvent = updateTitle(singleEvent, "Important meeting"); System.out.println("Event's new title is \"" + singleEvent.getTitle().getPlainText() + "\"."); // Demonstrate adding a reminder. Note that this will only work on a // primary calendar. singleEvent = addReminder(singleEvent, 15); System.out.println("Set a " + singleEvent.getReminder().get(0).getMinutes() + " minute reminder for the event."); // Demonstrate adding an extended property. singleEvent = addExtendedProperty(singleEvent); // Demonstrate deleting the entries. singleEvent.delete(); quickAddEvent.delete(); webContentEvent.delete(); recurringEvent.delete(); } catch (IOException e) { // Communications error System.err.println("There was a problem communicating with the service."); e.printStackTrace(); } catch (ServiceException e) { // Server side error System.err.println("The server had a problem handling your request."); e.printStackTrace(); } } /** * Prints the command line usage of this sample application. */ private static void usage() { System.out.println("Syntax: EventFeedDemo <username> <password>"); System.out.println("\nThe username and password are used for " + "authentication. The sample application will modify the specified " + "user's calendars so you may want to use a test account."); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -