【发布时间】:2014-06-03 09:38:09
【问题描述】:
我正在使用 Google Drive Android API(作为 Google Play 服务的一部分)将文件上传到云端。
要连接客户端,我使用以下代码(简化):
apiClient = new GoogleApiClient.Builder(context)
.addApi(Drive.API)
.setAccountName(preferences.getString("GOOGLE_DRIVE_ACCOUNT", null))
.build();
ConnectionResult connectionResult = apiClient.blockingConnect(SERVICES_CONNECTION_TIMEOUT_SEC, TimeUnit.SECONDS);
if (!connectionResult.isSuccess()) {
throw new ApiConnectionException(); //our own exception
}
要上传文件,我使用以下代码(简化):
DriveApi.ContentsResult result = Drive.DriveApi.newContents(apiClient).await();
if (!result.getStatus().isSuccess()) {
/* ... code for error handling ... */
return;
}
OutputStream output = result.getContents().getOutputStream();
/* ... writing to output ... */
//create actual file on Google Drive
DriveFolder.DriveFileResult driveFileResult = Drive.DriveApi
.getFolder(apiClient, folderId)
.createFile(apiClient, metadataChangeSet, result.getContents())
.await();
除了一个特定的用户案例外,一切都按预期工作。当用户从“连接的应用程序”(使用 Google 设置应用程序)中删除我们的应用程序时,此代码仍会为所有调用返回成功的结果。虽然文件从未上传到 Google Drive。
与 Google Play 服务的连接也成功。
这是 API 的错误还是可以通过某种方式检测到用户断开了应用程序?
【问题讨论】:
-
你能发布一个简单而完整的例子吗? (当然,没有您的 API 密钥)
-
@CheokYanCheng 我认为上面的代码应该足够了。
-
this code still returns successful results for all invocations表示apiClient.blockingConnect(...)仍然返回成功的结果?还是只有newContents和getContents().getOutputStream()? -
@ben75 是的,
apiClient.blockingConnect(...)也返回成功结果。 -
您是否尝试调用
listChildren()或queryChildren()并检查结果中是否存在文件?
标签: android google-play-services google-drive-android-api