Thu 21 Sep 2006
Using the Java Client Library with Google Calendar API - PART 2
Posted by anadeau under Developer Wing , Coding Tips & InfoHey Everyone,
Time for another post. I know it's been awhile but I've been pretty heavy into the Google API stuff. Honestly was kinda tricky to work with a first but I finally might be onto it... yeah, I'm just a bit of a slow learner I guess
In my last post I briefly mentioned using the Java Client Library. I've now got some code examples that might help some people who might have bumped into some of the same issues I was having (mostly grabbing an individual event from the Google Calendar). We are working on building a free online application called YourTeam. It's going to combine a community for Hockey Coaches, Players and Parents and also a Private Team area for teams to stay informed and have fun! If you'd like to read all about it and how excited we are about the new beta release about to come out, click here to read my brothers post!
Here is the code that I was pulling my hair out for a few days over. These examples use the client library and are Java Server Pages. Enjoy! I hope they help. You will have to replace a few variables to get it working with your own code (ie: ACCOUNTNAME is your Google account name)
-
try{
-
//get connection
-
GoogleService myService = new GoogleService("cl", str_service);
-
myService.setUserCredentials(ACCOUNTNAME, ACCOUNTPASSWORD);
-
-
//create event here
-
EventEntry myEntry = new EventEntry();
-
myEntry.setTitle(new PlainTextConstruct(request.getParameter("event_name")));
-
myEntry.setContent(new PlainTextConstruct(request.getParameter("event_desc")));
-
-
//these are your date values for the calendar
-
DateTime startTime = DateTime.parseDateTime("2006-09-14T15:00:00-08:00");
-
DateTime endTime = DateTime.parseDateTime("2006-09-14T17:00:00-08:00");
-
-
When eventTimes = new When();
-
eventTimes.setStartTime(startTime);
-
eventTimes.setEndTime(endTime);
-
myEntry.addTime(eventTimes);
-
-
Where eventWhere = new Where();
-
eventWhere.setValueString(request.getParameter("event_location"));
-
myEntry.addLocation(eventWhere);
-
-
// Send the request and receive the response:
-
EventEntry insertedEntry = (EventEntry)myService.insert(postUrl, myEntry);
-
str_msg="Event Added Successfully.";
-
}
-
str_msg="Error adding Google Calendar Event:"+ex;
-
}
Note: This code might not compile, I tried to take out a lot of code to shorten it but it should be a good starting point.
-
try{
-
//get connection
-
CalendarQuery myQuery = new CalendarQuery(feedUrl);
-
-
String str_start="2006-09-14T00:00:00";
-
String str_end="2006-09-21T23:59:59";
-
-
myQuery.setMinimumStartTime(DateTime.parseDateTime(str_start));
-
myQuery.setMaximumStartTime(DateTime.parseDateTime(str_end));
-
myQuery.addCustomParameter(new Query.CustomParameter("orderby", "starttime"));
-
myQuery.addCustomParameter(new Query.CustomParameter("sortorder", "ascending"));
-
myQuery.addCustomParameter(new Query.CustomParameter("singleevents", "true"));
-
-
CalendarService myService = new CalendarService(str_service);
-
myService.setUserCredentials(ACCOUNTNAME, ACCOUNTPASS);
-
-
// Send the request and receive the response:
-
Feed resultFeed = (Feed)myService.query(myQuery, Feed.class);
-
-
new EventFeed().declareExtensions(myService.getExtensionProfile());
-
EventFeed calFeed = (EventFeed) myService.query(myQuery,EventFeed.class);
-
-
EventEntry calEntry = null;
-
-
if (calFeed.getEntries().size()> 0) {
-
-
for (int n=0; n<calFeed.getEntries().size();n++){
-
calEntry = (EventEntry) calFeed.getEntries().get(n);
-
-
//grabs description for event
-
TextContent tc = (TextContent)calEntry.getContent();
-
PlainTextConstruct ptc = (PlainTextConstruct)tc.getContent();
-
-
calEntry.getTitle().getPlainText();
-
lTimes=calEntry.getTimes();
-
When when =null;
-
-
when = (When)iterator1.next();
-
strTempStart=when.getStartTime().toUiString();
-
strTempStart=strTempStart.substring(strTempStart.indexOf(" "),strTempStart.length());
-
-
strTempEnd=when.getEndTime().toUiString();
-
strTempEnd=strTempEnd.substring(strTempEnd.indexOf(" "),strTempEnd.length());
-
strTempEnd=convertTime(strTempEnd);
-
}
-
-
lLocs=calEntry.getLocations();
-
Where where = (Where)iterator1.next();
-
where.getValueString();
-
}
-
}
-
}
-
str_msg="Error viewing Team Google Calendar:"+ex;
-
}
-
//get connection
-
CalendarService calendarService = new CalendarService(str_service);
-
calendarService.setUserCredentials(ACCOUNTNAME, ACCOUNTPASSWORD);
-
-
//call calEntry.getSelfLink().getHref() to get the url
-
//example: http://www.google.com/calendar/feeds/ACCOUNTNAME/private/full/d1gn05stk3175uthp30s3et3is
-
-
// Mark the feed as an Event feed:
-
final ExtensionProfile profile = calendarService.getExtensionProfile();
-
new EventFeed().declareExtensions(profile);
-
final EventFeed feed = (EventFeed)calendarService.getFeed(entryUrl, EventFeed.class);
-
-
EventEntry retrievedEntry = (EventEntry)calendarService.getEntry(entryUrl, EventEntry.class);
-
retrievedEntry.setTitle(new PlainTextConstruct(request.getParameter("event_name")));
-
retrievedEntry.setContent(new PlainTextConstruct(request.getParameter("event_desc")));
-
-
//same date format as adding
-
DateTime startTime = DateTime.parseDateTime(str_start_time);
-
DateTime endTime = DateTime.parseDateTime(str_end_time);
-
-
When when =null;
-
when = (When)iterator1.next();
-
when.setStartTime(startTime);
-
when.setEndTime(endTime);
-
}
-
-
Where where = (Where)iterator1.next();
-
where.setValueString(request.getParameter("event_location"));
-
}
-
-
// Update the info
-
// call retrievedEntry.getEditLink().getHref() to get the edit url like this: http://www.google.com/calendar/feeds/info%40webscope.ca/private/full/d1gn05stk3175uthp30s3et3is/63294552679
-
EventEntry updatedEntry = (EventEntry)calendarService.update(editUrl, retrievedEntry);
-
str_msg="Event Updated Successfully.";
So, hopefully that code can help someone out there. It might be a bit messy and long to look at but if you grab it and use it along with the Tutorials from Google I hope it can start working. Up next for me will be deleting Google Calendar Events from our webapp (YourTeam) and then hopefully some other enhancements!
So, if anyone has any questions about the information above let me know and I'll try my best
and if anyone knows any hockey coaches, players or fans out there that are looking for a great, fun team software package send them to YourTeam!

Cheers,
Adrian
November 16th, 2006 at 3:33 pm
Thanks, this really helped me in the development of a simple app that downloads the events for the next four weeks, and lets you add events offline that are uploaded when you come back online. Please post more code examples when you have em!
cheers
/Andie
December 1st, 2006 at 10:39 pm
No problem, glad it helped out.
– Adrian
March 5th, 2007 at 6:08 am
Hi,
Your code about the date range query will probably help me fix a bug in my code (use the extensions!).
One question though, the query is executed twice in your code, is there a reason for this or just a copy/paste error?
Here is the code I’m talking about:
// Send the request and receive the response:
Feed resultFeed = (Feed)myService.query(myQuery, Feed.class);
new EventFeed().declareExtensions(myService.getExtensionProfile());
EventFeed calFeed = (EventFeed) myService.query(myQuery,EventFeed.class);
March 5th, 2007 at 9:00 am
Hello Mike,
That’s a good question. It’s been quite awhile since I worked on the Google Calendar API code. I think the first line:
Feed resultFeed = (Feed)myService.query(myQuery, Feed.class);
has to be there but I guess you could try it without. Obviously if it doesn’t have to be there it would be a good idea to remove it, one less call. Let me know if you try it without and I can update the code also.
Adrian
March 5th, 2007 at 5:21 pm
Hi,
I tried and could get it to work even without the extensions.
Turns out the projection of my URL was basic instead of full. YMMV but the first link should be safe to remove, the extension line might work, you’ll have to test it
// Send the request and receive the response:
/************************
Feed resultFeed = (Feed)calendarService.query(myQuery, Feed.class);
new EventFeed().declareExtensions(calendarService.getExtensionProfile());
*********************/
calFeed = (EventFeed)
calendarService.query(myQuery,EventFeed.class);