【问题标题】:batch adding events to google calender批量添加事件到谷歌日历
【发布时间】:2013-01-16 07:51:47
【问题描述】:

您好,我正在尝试使用 google-api-java-client 和 android 应用程序的日历 API 服务将事件添加到日历中。我使用 Yaniv Inbar 创建的 calendersample 项目作为模板,效果很好。将 1 个事件插入所选日历时效果很好,但是当我尝试将事件批量添加到日历时,会出现非法状态异常。

在示例中,您可以像这样批量添加日历。 全班可以在这里找到AsyncBatchInsertCalendars.java

@Override
protected void doInBackground() throws IOException {
   BatchRequest batch = client.batch();
   for (Calendar calendar : calendars) {
      client.calendars().insert(calendar).setFields(CalendarInfo.FIELDS)
      .queue(batch, new JsonBatchCallback<Calendar>() {
         public void onSuccess(Calendar calendar, GoogleHeaders headers) {
            model.add(calendar);
         }
         @Override
         public void onFailure(GoogleJsonError err, GoogleHeaders headers)
         throws IOException {
            Utils.logAndShowError(activity, CalendarSampleActivity.TAG, err.getMessage());
         }
      });
   }
   batch.execute();
}

我重写了课程,使它成为事件而不是日历。如果您查看整个课程AsyncBatchInsertEvent.java,您会发现在 doInBackground 方法中,我还循环遍历了一个创建事件列表的数组列表。应该将其添加到要插入给定日历的批次中。

@Override
protected void doInBackground() throws IOException {
   BatchRequest batch = client.batch();
   for (Event event : events) {
      client.events().insert(calender.id, event).queue(batch, 
      new JsonBatchCallback<Event>() {
         public void onSuccess(Event event, GoogleHeaders headers) {
            //TODO show succes message.
         }
         @Override
         public void onFailure(GoogleJsonError err, GoogleHeaders headers)
         throws IOException {
            Utils.logAndShowError(activity, EventActivity.TAG, err.getMessage());
         }
      });
   }
   batch.execute();
}

如果我使用它,则会出现异常并且应用程序崩溃

W/dalvikvm(21030): threadid=20: thread exiting with 
uncaught exception (group=0x40c19930)
E/AndroidRuntime(21030): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(21030): java.lang.RuntimeException: An error occured 
while executing doInBackground()

可以在 pastebin log.txt 找到错误的完整堆栈跟踪。有谁知道如何解决这个问题或者我是否错误地实现了代码?完整的代码可以在 Pastbin AsyncBatchInsertEvent.java找到这里

【问题讨论】:

  • 我对此进行了更多研究,似乎是 batch.execute();错误原因批次包含 0 个排队项目。当我运行 batch.size() 输出为 0

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


【解决方案1】:

愚蠢的我,arraylist 事件是空的,因为我检查了 string == string 而不是 string.equels(string)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多