Google Api Java客户端驱动器文件搜索查询

时间:2014-06-12 11:25:15

标签: java google-drive-api google-api-java-client

因此,使用REST API,您可以列出像

这样的文件夹子项
final StringBuilder searchQuery = new StringBuilder();
searchQuery.append('\'').append(parentResourceId).append('\'').append(" in parents");
searchQuery.append(" and mimeType=\'").append(DriveFolder.MIME_TYPE).append('\'');
searchQuery.append(" and trashed=false and title=\'").append(driveFolderName).append('\'');

final Uri url =
        Uri.parse(FILES_URL).buildUpon().appendQueryParameter("q", searchQuery.toString()).build();

final HttpGet get = new HttpGet(url.toString());

现在在Google Api Java Client中,我看不出我该怎么做。

我现在找到的是

@NonNull
public static Set<String>
        listChildrenIds(@NonNull final Drive service, @NonNull final String parentResId)
                throws IOException {
    final Set<String> result = new HashSet<>();
    final Children.List request = service.children().list(parentResId);
    final ChildList list = request.execute();
    for (ChildReference child : list.getItems()) {
        final String id = child.getId();
        if (id != null) {
            result.add(id);
        }
    }
    return result;
}

@Nullable
public static File find(@NonNull final Drive service,
        @NonNull final Collection<String> ids,
        @NonNull final String title,
        @Nullable final String mimeType) throws IOException {
    for (final String id : ids) {
        final File file = service.files().get(id).execute();
        if (file.getTitle().equals(title)) {
            if (mimeType == null || file.getMimeType().equals(mimeType)) {
                return file;
            }
        }
    }
    return null;
}

@Nullable
public static File findFile(@NonNull final Drive service,
        @NonNull final String title,
        @NonNull final String parentId,
        @Nullable final String mimeType) throws IOException {
    final Set<String> children = listChildrenIds(service, parentId);
    return find(service, children, title, mimeType);
}

但是这会为每个文件提供额外的请求。而且这种方式我必须手动过滤掉已删除的文件。

1 个答案:

答案 0 :(得分:2)

找到它。我在get()搜索,所以我暂时找不到它。它似乎是list()

driveService.files().list().setQ(query).execute();