Android 日历 API 插入重复事件
使用 Google 日历 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 的 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
。
不知道为什么他们没有被通过......
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 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 DateTime 对象创建 EventDateTime 时,似乎必须再次添加 TimeZone。以下工作有效:
It seems that one must add the TimeZone again when creating an EventDateTime from the DateTime object. The following worked: