Android Calendar API会插入重复出现的事件

时间:2012-01-13 19:55:03

标签: java android google-calendar-api google-api-java-client recurrence

使用Google Calendar Api

浏览rfc2445后,我仍无法设置事件的重复发生。

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

运行上面的代码后,我一直收到这个错误(注意:如果我省略指定重复,一切正常,但显然这会使事件单一发生):

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)

我在日历中手动创建了一个定期事件,并提供了一个简单的方法来读取它。执行event.getRecurrence时。重现是"RRULE:FREQ=WEEKLY;WKST=MO;BYDAY=MO",它准确地表明我创建的事件是每周一次在周一重复出现。

有人能找到我出错的地方吗?

修改

使用Google's Api Explorer,我手动创建了带有重复字段的JSON并且它有效。

在记录从event创建的JSON之后,似乎没有传递时区。

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

此外,执行event.getStart().getTimeZone().toString()导致java.lang.NullPointerException

不确定为什么他们没有被传递......

1 个答案:

答案 0 :(得分:6)

似乎必须在从DateTime对象创建EventDateTime时再次添加TimeZone。以下工作:

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");