我正在尝试从今天开始删除所有日历条目。我运行查询然后在查询结果上调用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文档?
答案 0 :(得分:9)
您可以使用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)
答案 1 :(得分: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());
}
答案 2 :(得分:2)
API页面上有一个小引号 http://code.google.com/apis/calendar/data/1.0/reference.html#Parameters
注意:Calendar的max-results查询参数默认设置为25, 这样你就不会得到一整个 日历饲料意外。如果你想 你可以收到整个饲料 指定一个非常大的数字 最大结果。
因此,为了从Google日历Feed中获取所有活动,我们会这样做:
google.calendarurl.com/.../basic?max-results=999999
在API中,您也可以使用setMaxResults = 999999
进行查询答案 3 :(得分:1)
我在搜索Python解决方案时来到这里; 如果有人以同样的方式陷入困境,重要的是第四行:
query = gdata.calendar.service.CalendarEventQuery(cal, visibility, projection)
query.start_min = start_date
query.start_max = end_date
query.max_results = 1000
答案 4 :(得分:0)
不幸的是,Google会限制您可以检索的最大查询数。这样可以使查询调控器保持其指南(例如,不允许HTTP请求超过30秒)。他们围绕这个构建了他们的整个架构,所以你不妨建立你的逻辑。