【问题标题】:Google calendar query returns at most 25 entriesGoogle 日历查询最多返回 25 个条目
【发布时间】:2011-02-08 10:51:22
【问题描述】:

我正在尝试删除从今天开始的所有日历条目。我运行一个查询,然后在查询结果上调用 getEntries()。 getEntries() 总是返回 25 个条目(如果日历上的条目少于 25 个,则返回更少)。为什么没有返回所有条目?我预计大约有 80 个条目。

作为测试,我尝试运行查询,删除返回的 25 个条目,再次运行查询,再次删除等。这可行,但必须有更好的方法。

下面是只运行一次查询的 Java 代码。

CalendarQuery myQuery = new CalendarQuery(feedUrl);

DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'");
Date dt = Calendar.getInstance().getTime();

myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt)));
// Make the end time far into the future so we delete everything
myQuery.setMaximumStartTime(DateTime.parseDateTime("2099-12-31T23:59:59"));

// Execute the query and get the response
CalendarEventFeed resultFeed = service.query(myQuery, CalendarEventFeed.class);

// !!! This returns 25 (or less if there are fewer than 25 entries on the calendar) !!!
int test = resultFeed.getEntries().size();

// Delete all the entries returned by the query
for (int j = 0; j < resultFeed.getEntries().size(); j++) {
   CalendarEventEntry entry = resultFeed.getEntries().get(j);

   entry.delete();
}

PS:我看过Data API Developer's Guide 和Google Data API Javadoc。这些网站还可以,但不是很好。有人知道其他 Google API 文档吗?

【问题讨论】:

    标签: gdata-api google-calendar-api


    【解决方案1】:

    您可以使用myQuery.setMaxResults() 增加结果数量。不过会有一个最大值,因此您可以通过改变myQuery.setStartIndex() 进行多个查询(“分页”结果)。

    http://code.google.com/apis/gdata/javadoc/com/google/gdata/client/Query.html#setMaxResults(int) http://code.google.com/apis/gdata/javadoc/com/google/gdata/client/Query.html#setStartIndex(int)

    【讨论】:

    • 如果它对任何人有用,我将我的多页查询代码作为单独的答案包含在内。这样人们就可以看到编码的解决方案。
    【解决方案2】:

    很遗憾,Google 将限制您可以检索的最大查询数。这是为了让查询调控器保持在他们的指导方针中(例如,HTTP 请求的时间不允许超过 30 秒)。他们已经围绕这个构建了他们的整个架构,所以你也可以像你一样构建逻辑。

    【讨论】:

      【解决方案3】:

      根据 Jim Blackler 和 Chris Kaminski 的回答,我增强了代码以读取页面中的查询结果。我也是批量删除,应该比单独删除要快。

      我在这里提供 Java 代码,以防它对任何人有用。

      CalendarQuery myQuery = new CalendarQuery(feedUrl);
      
      DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'"); 
      Date dt = Calendar.getInstance().getTime(); 
      
      myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt))); 
      // Make the end time far into the future so we delete everything 
      myQuery.setMaximumStartTime(DateTime.parseDateTime("2099-12-31T23:59:59")); 
      
      // Set the maximum number of results to return for the query.
      // Note: A GData server may choose to provide fewer results, but will never provide
      // more than the requested maximum.
      myQuery.setMaxResults(5000);
      int startIndex = 1;
      int entriesReturned;
      
      List<CalendarEventEntry> allCalEntries = new ArrayList<CalendarEventEntry>();
      CalendarEventFeed resultFeed;
      
      // Run our query as many times as necessary to get all the
      // Google calendar entries we want
      while (true) {
          myQuery.setStartIndex(startIndex);
      
          // Execute the query and get the response
          resultFeed = service.query(myQuery, CalendarEventFeed.class);
      
          entriesReturned = resultFeed.getEntries().size();
          if (entriesReturned == 0)
              // We've hit the end of the list
              break;
      
          // Add the returned entries to our local list
          allCalEntries.addAll(resultFeed.getEntries());
      
          startIndex = startIndex + entriesReturned;
      }
      
      // Delete all the entries as a batch delete
      CalendarEventFeed batchRequest = new CalendarEventFeed();
      
      for (int i = 0; i < allCalEntries.size(); i++) {
          CalendarEventEntry entry = allCalEntries.get(i);
      
          BatchUtils.setBatchId(entry, Integer.toString(i));
          BatchUtils.setBatchOperationType(entry, BatchOperationType.DELETE);
          batchRequest.getEntries().add(entry);
      }
      
      // Get the batch link URL and send the batch request
      Link batchLink = resultFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
      CalendarEventFeed batchResponse = service.batch(new URL(batchLink.getHref()), batchRequest);
      
      // Ensure that all the operations were successful
      boolean isSuccess = true;
      StringBuffer batchFailureMsg = new StringBuffer("These entries in the batch delete failed:");
      for (CalendarEventEntry entry : batchResponse.getEntries()) {
          String batchId = BatchUtils.getBatchId(entry);
          if (!BatchUtils.isSuccess(entry)) {
              isSuccess = false;
              BatchStatus status = BatchUtils.getBatchStatus(entry);
              batchFailureMsg.append("\nID: " + batchId + "  Reason: " + status.getReason());
          }
      }
      
      if (!isSuccess) {
          throw new Exception(batchFailureMsg.toString());
      }
      

      【讨论】:

        【解决方案4】:

        API 页面上有一个小引用 http://code.google.com/apis/calendar/data/1.0/reference.html#Parameters

        注意:Calendar 的 max-results 查询参数默认设置为 25, 这样你就不会收到一个完整的 日历饲料意外。如果你想 要接收整个提要,您可以 指定一个非常大的数字 最大结果。

        因此,要从 google 日历提要中获取所有事件,我们这样做:

        google.calendarurl.com/.../basic?max-results=999999

        在 API 中您也可以使用 setMaxResults=999999 进行查询

        【讨论】:

        • 据记录,Google 将接受作为参数的最高数字是 2,147,483,647。这是 32 位操作系统上带符号整数的最大正值。
        【解决方案5】:

        我是在寻找 Python 解决方案时来到这里的; 如果有人陷入同样的​​困境,重要的是第四行:

        query = gdata.calendar.service.CalendarEventQuery(cal, visibility, projection)
        query.start_min = start_date
        query.start_max = end_date 
        query.max_results = 1000
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-11-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多