上传文件到谷歌驱动器时检测冲突 [英] Detect conflict when uploading document to Google Drive

查看:589
本文介绍了上传文件到谷歌驱动器时检测冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序将更新谷歌驱动器文件。该文件可以在其他地方也被修改(例如,通过驱动网络接口),所以有可能是在文件上传冲突。然而,这应该很少发生。这就是为什么我不希望我的应用程序首先查询修订历史记录(因为这是在大多数情况下不必要的),只有在这之后,更新文件。 如何检测到有更新文件时有冲突?

My Android app updates a Google Drive document. The file can be modified also elsewhere (e.g. through Drive web interface), so there may be a conflict on file upload. However, this should rarely happen. That's why I don't want my app to first query the revision history (since this is in most cases unnecessary) and only after that, update the file. How can I detect that there is a conflict when updating the file?

我的调查至今显示, getHeadRevisionId()返回null 虽然的空最新修订版ID已经被报道固定。 另一件事我想是setEtag()上的文件更新前()。 它应该给我上更新的错误,但上载成功,即使该文件已被远程更改!这是使用ETag的正确方法是什么?

My investigation so far reveals that getHeadRevisionId() returns null although the null head revision id has been reported fixed. Another thing I tried was setEtag() on the file before update(). It should have given me error on update, but the upload was successful even the file had been changed remotely! Is this the correct way to use ETag?

推荐答案

设置就算匹配 HTTP标头:

final Update update = mService.files().update(mDriveFile.getId(), mDriveFile, byteContent);
final HttpHeaders headers = update.getRequestHeaders();
headers.setIfMatch(mDriveFile.getEtag());
update.setRequestHeaders(headers);
mDriveFile = update.execute();

在情况下,文档同时改变,更新将得到一个反应是这样拒绝了:

In case the document has changed meanwhile, the update will get rejected with a response something like:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 412 Precondition Failed
{
  "code": 412,
  "errors": [
    {
      "domain": "global",
      "location": "If-Match",
      "locationType": "header",
      "message": "Precondition Failed",
      "reason": "conditionNotMet"
    }
  ],
  "message": "Precondition Failed"
}

请注意,的ETag可能会改变,即使内容不的。

这篇关于上传文件到谷歌驱动器时检测冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆