With a little side of applesauce...

Thursday, March 20, 2008

Java/gdata - List Calendars

As a total Java wimp, I was pretty excited when this example worked:
import com.google.gdata.client.*;
import com.google.gdata.client.calendar.*;
import com.google.gdata.data.*;
import com.google.gdata.data.acl.*;
import com.google.gdata.data.calendar.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;
import java.net.URL;
import java.io.IOException;
import javax.xml.*;


public final class GCalendar {
public static void main(final String[] args) {
// need to set service argument to [comp-id]-[app-name]-[version-number]
// this is how google monitors who is sending traffic, (and probably how they throttle).
CalendarService myService = new CalendarService("myaccount-test-1.0");
try {
myService.setUserCredentials("myaccount@gmail.com", "password");
} catch (AuthenticationException e) {
System.out.println("\tUnable to authenticate: " + e);
}

try {
URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/allcalendars/full");
System.out.println(feedUrl);
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());
}
} catch (java.net.MalformedURLException e) {
System.out.println("\tWe have a bad URL: " + e);
} catch (IOException e) {
System.out.println("\tWe were not able to get the feed: " + e);
} catch (ServiceException e) {
System.out.println("\tService Not Available: " + e);
}
}
}


It is the example from Google's "Getting Started with the Java Client Library", *but* with the error handling. I hope this helps others are just starting out.

No comments: