5
votes

Using the Google Calendar Api.

After browsing the rfc2445, I'm still unable to set recurrence on an event.

private String[] days = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; 
private String rrule = "RRULE:FREQ=WEEKLY;WKST=MO;BYDAY=";
private Event createdEvent;
...

Event event = new Event();
StringBuilder sb = new StringBuilder();
sb.append(rrule);
sb.append(days[startTime.get(java.util.Calendar.DAY_OF_WEEK)-1]);
event.setSummary("HELLO WORLD");
event.setLocation("");

DateTime start = new DateTime(startTime.getTime(), TimeZone.getTimeZone("UTC"));
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(endTime.getTime(), TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end)); 

//Setting Recurrence
ArrayList<String> recur = new ArrayList<String>();
recur.add(sb.toString());
event.setRecurrence(recur);

createdEvent = cal.events().insert("primary", event).execute();  //line 167

After running the above code, I keep receiving this error (NB: everything works if I omit specifying the recurrence, though obviously this makes the event single-occurring):

01-13 19:26:17.190: WARN/System.err(5732): com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
01-13 19:26:17.190: WARN/System.err(5732): {
01-13 19:26:17.190: WARN/System.err(5732):   "code" : 400,
01-13 19:26:17.190: WARN/System.err(5732):   "errors" : [ {
01-13 19:26:17.190: WARN/System.err(5732):     "domain" : "global",
01-13 19:26:17.190: WARN/System.err(5732):     "message" : "Required",
01-13 19:26:17.190: WARN/System.err(5732):     "reason" : "required"
01-13 19:26:17.190: WARN/System.err(5732):   } ],
01-13 19:26:17.190: WARN/System.err(5732):   "message" : "Required"
01-13 19:26:17.190: WARN/System.err(5732): }
01-13 19:26:17.190: WARN/System.err(5732):     at com.google.api.client.googleapis.services.GoogleClient.execute(GoogleClient.java:123)
01-13 19:26:17.190: WARN/System.err(5732):     at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:67)
01-13 19:26:17.190: WARN/System.err(5732):     at com.google.api.services.calendar.Calendar$Events$Insert.execute(Calendar.java:2308)
01-13 19:26:17.190: WARN/System.err(5732):     at com.example.myproject.className.run(className.java:167)
01-13 19:26:17.190: WARN/System.err(5732):     at java.lang.Thread.run(Thread.java:1020)

I manually created a recurring event in the Calendar, and made a simple method to read it in. When performing event.getRecurrence. The recurrence was "RRULE:FREQ=WEEKLY;WKST=MO;BYDAY=MO", which accurately stated that the event that I had created is weekly recurring on a Monday.

Can anybody spot where I'm going wrong?

EDIT:

Using Google's Api Explorer, I manually created the JSON with the recurrence field and it worked.

After Logging the JSON being created from event, it seems that the timezone isn't being passed.

{end={dateTime=2012-01-16T09:50:00.000Z}, location=, recurrence=[RRULE:FREQ=WEEKLY;], start={dateTime=2012-01-16T09:00:00.000Z}, summary=HELLO WORLD}

Additionally, performing event.getStart().getTimeZone().toString() caused java.lang.NullPointerException.

Not sure why they aren't being passed ...

1

1 Answers

6
votes

It seems that one must add the TimeZone again when creating an EventDateTime from the DateTime object. The following worked:

DateTime start = new DateTime(startTime.getTime(), TimeZone.getTimeZone("UTC"));
event.setStart(new EventDateTime().setDateTime(start).setTimeZone("UTC");
DateTime end = new DateTime(endTime.getTime(), TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end).setTimeZone("UTC");