How would one go about parsing the start and end times of events in a users Google Calendar using the google-api-java-client?
After installing this sample android project from Google code, I can get into my Google calendar and parse some information, (like all the calendars, the event names, when it was published, and the summary), but I cannot for the life of me get the event start and end times.
My understanding of the code is as such.
Inside the main activity class (CalendarAndroidSample.java) this is the method that gets the title for each of my calendars:
void executeRefreshCalendars() {
String[] calendarNames;
List<CalendarEntry> calendars = this.calendars;
calendars.clear();
try {
CalendarUrl url = CalendarUrl.forAllCalendarsFeed();
// page through results
while (true) {
CalendarFeed feed = client.executeGetCalendarFeed(url);
if (feed.calendars != null) {
calendars.addAll(feed.calendars);
}
String nextLink = feed.getNextLink();
if (nextLink == null) {
break;
}
}
int numCalendars = calendars.size();
calendarNames = new String[numCalendars];
for (int i = 0; i < numCalendars; i++) {
calendarNames[i] = calendars.get(i).title;
}
} catch (IOException e) {
handleException(e);
calendarNames = new String[] {e.getMessage()};
calendars.clear();
}
The for-loop above is assigning the title of each calendar in my account to the string array "calendarNames[]."
I have figured out that within a separete java file inside the project (Entry.java) found here, the @Key annotation directs the code to parse an XML element and the name of the string should match the name of the element.
public class Entry implements Cloneable {
@Key
public String summary;
@Key
public String title;
@Key
public String updated;
@Key
public String published;
@Key("link")
public List<Link> links;
@Override
protected Entry clone() {
try {
@SuppressWarnings("unchecked")
Entry result = (Entry) super.clone();
Data.deepCopy(this, result);
return result;
} catch (CloneNotSupportedException e) {
throw new IllegalStateException(e);
}
}
String getEditLink() {
return Link.find(links, "edit");
}
}
So....
@Key
public String published;
...would find the element in the XML named "published" and assign the value of that element to the string.
Thus, returning to the first referenced java method executeRefreshCalendars() (inside CalendarAndroidSample.java), changing
calendarNames[i] = calendars.get(i).title;
to
calendarNames[i] = calendars.get(i).published;
gives me the date the event was published.
I think my problem in regard to understanding this code is that for an event's start and end time, the data lies within an XML element that has two parts.
Can anyone help me gain some insight on how to do this? I have more than 10 tabs open in my browsers and have looked everywhere on SO for help on this and the closest thing I can find to helping me is this post, but I can't figure out how to implement it with the sample project I'm working with.
Thanks.
You'll need to use the EventFeed and take a look at the EventEntry class
http://code.google.com/p/google-api-java-client/source/browse/calendar-v2-atom-oauth-sample/src/com/google/api/client/sample/calendar/v2/model/EventEntry.java?repo=samples
The Atom string returned containing the startTime / endTime will look like this :
It's modelled in the EventEntry class like this :
(a property for the When object, mapped using the @Key annotation)
The When object, models the start/endTime attributes on the When object
Client code when inteacting with the eventFeed will look like this :