gpt4 book ai didi

android google drive api - 查询文件第一次返回0文件,第二次就可以了

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:54 25 4
gpt4 key购买 nike

我有 android 应用程序,它在谷歌驱动器上创建文件和读取文件(设备之间同步 sql lite db)。创建文件运行正常。读取文件也可以,但仅用于第二次尝试。第一次我的查询总是返回 0。看起来第一次查询只检查本地存储?

示例:我从移动设备创建导出。没关系。我可以看到该文件已创建,例如我通过谷歌驱动器上的网络看到它。我也可以通过 android 驱动器应用程序看到它。所以我可以从我的应用程序从我的平板电脑导入这个文件。第一次尝试每次都失败。查询找不到文件。第二次尝试:找到并导入了文件。为什么会出现这种行为?

查询是这样创建的:

Query query = new Query.Builder().addFilter(Filters.and(
Filters.eq(SearchableField.MIME_TYPE, "text/xml"),
Filters.eq(SearchableField.TITLE,
getResources().getString(R.string.app_file_name)),
Filters.eq(SearchableField.TRASHED, false))).build();

Drive.DriveApi.query(mGoogleApiClient, query)
.setResultCallback(metadataCallback);

读取文件的回调结果是这样的:

MetadataBuffer mdbf = null;
mdbf = result.getMetadataBuffer();
int iCount = mdbf.getCount();
tvout("file count: "+String.valueOf(iCount));
if (iCount == 1){
myFileId = mdbf.get(0).getDriveId();
Log.i(LOG_TAG, "file was found");
readFile();
}
else
{
displayAlertBox(getApplicationContext().getResources()
.getString(R.string.import_alert_res_not_ok)+" (ER-NOFILE)");
}

我这样做就像在 google drive api 示例 - 查询文件中实现的那样。我也用同步功能做了很多测试。但是每次我的 iCount 第一次都是 0。第二次是 1. 找到文件。

最佳答案

这是因为,本地同步需要一点时间来处理。如果您在完成之前提出请求,您可能会得到不完整的结果。您可以使用 requestSync等待同步完成。

Drive.DriveApi.requestSync(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.getStatus().isSuccess()) {
// Do you work here
}
}
});
}

请注意,不要过于频繁地尝试同步操作。

In order to avoid excessive load on the device and the server, sync requests are rate limited. In this case, the operation will fail with DRIVE_RATE_LIMIT_EXCEEDED status, which indicates that a sync already happened quite recently so there is no need for another sync. The operation will succeed when reattempted after a sufficient backoff duration.

关于android google drive api - 查询文件第一次返回0文件,第二次就可以了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28624542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com