【问题标题】:Detect conflict when uploading document to Google Drive将文档上传到 Google Drive 时检测冲突
【发布时间】:2014-02-15 05:39:26
【问题描述】:

我的 Android 应用更新了 Google Drive 文档。该文件也可以在其他地方修改(例如通过 Drive Web 界面),因此文件上传可能存在冲突。但是,这应该很少发生。这就是为什么我不希望我的应用程序首先查询修订历史记录(因为这在大多数情况下是不必要的),然后才更新文件。 如何检测更新文件时是否存在冲突?

到目前为止,我的调查显示getHeadRevisionId() returns null 尽管null head revision id has been reported fixed。 我尝试的另一件事是在 update() 之前对文件设置 setEtag()。 It should have given me error on update,但是远程修改了文件还是上传成功了!这是使用 ETag 的正确方法吗?

【问题讨论】:

    标签: android google-drive-api google-docs merge-conflict-resolution


    【解决方案1】:

    设置If-Match 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();
    

    如果文档同时发生更改,更新将被拒绝并返回如下响应:

    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 might change even if the content does not

    【讨论】:

    【解决方案2】:

    “这是使用 ETag 的正确方法吗?”

    是的

    此外,对于非 Docs 文件,您还应该检查 md5Checksum 以了解内容的更改。

    【讨论】:

    • 那么,如果是使用ETag的正确方法,为什么我的文件更新没有失败?
    • 你能粘贴http请求和响应吗?
    • 我怀疑 setEtag() 没有设置 If-Match 标头。您可能需要手动设置它。看到 http 请求应该可以确认这一点。
    • 几个问题: 1. 我如何记录标题(尝试stackoverflow.com/a/3303131/1097104 没有成功)? 2.如何手动设置标题,我可以将它设置为我的 mService.files().update(...).execute() 命令的一部分吗?
    • (1) 参见code.google.com/p/google-http-java-client/wiki/Android#Logging (2) Java 客户端库使您能够访问底层的 http 传输。我找不到为您粘贴的快速链接。这实际上是一个单独的 SO 问题的好主题。
    猜你喜欢
    • 2021-07-20
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多