使用 google api java 客户端更新 google docs 文档
我已经获得了几乎完整的 API 来工作,可以创建和删除文档和文件夹。但我无法更新文档。使用 gdata 时非常简单,但由于此代码必须在所有 Android 设备上运行,所以我必须使用 google api java 客户端。这是我测试更新的方法:
public void updateTest() throws IOException {
InputStreamContent isContent = new InputStreamContent();
isContent.inputStream = new ByteArrayInputStream("NEW CONTENT".getBytes("UTF-8"));
isContent.type = "text/plain";
HttpRequest request = transport.buildPostRequest();
request.setUrl("https://docs.google.com/feeds/default/media/document:0A[snip]3Y");
request.content = isContent;
// request.headers.set("If-Match", "*");
try {
request.execute().parseAs(DocumentListEntry.class);
} catch (HttpResponseException e) {
if (Constant.DEBUG) Log.d(TAG, "error: " + e.response.parseAsString());
throw e;
} catch (ClientProtocolException e) {
if (Constant.DEBUG) Log.d(TAG, "error: " + e.getMessage());
throw e;
}
}
发生的情况是我只是创建一个新文档(使用给定的内容,创建一个新文档效果很好)。如果我添加“If-Match:*”标头,我会得到以下异常:
11-19 11:17:16.536: DEBUG/DocsAPI(32195): error: <errors xmlns='http://schemas.google.com/g/2005'>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): <error>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): <domain>GData</domain>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): <code>noPostConcurrency</code>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): <internalReason>POST method does not support concurrency</internalReason>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): </error>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): </errors>
11-19 11:17:16.536: WARN/System.err(32195): com.google.api.client.http.HttpResponseException: 501 Not Implemented
11-19 11:17:16.540: WARN/System.err(32195): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:209)
...
I have gotten almost a complete API to work, with creating and deleting documents and folders. But I fail at updating documents. It was very easy when using gdata, but since this code has to work on all android devices, I have to use google api java client. Here is a method where I test the updating:
public void updateTest() throws IOException {
InputStreamContent isContent = new InputStreamContent();
isContent.inputStream = new ByteArrayInputStream("NEW CONTENT".getBytes("UTF-8"));
isContent.type = "text/plain";
HttpRequest request = transport.buildPostRequest();
request.setUrl("https://docs.google.com/feeds/default/media/document:0A[snip]3Y");
request.content = isContent;
// request.headers.set("If-Match", "*");
try {
request.execute().parseAs(DocumentListEntry.class);
} catch (HttpResponseException e) {
if (Constant.DEBUG) Log.d(TAG, "error: " + e.response.parseAsString());
throw e;
} catch (ClientProtocolException e) {
if (Constant.DEBUG) Log.d(TAG, "error: " + e.getMessage());
throw e;
}
}
What happens is that I just create a new document (with the given content, creating a new document works perfect). If I do add the "If-Match: *"-header, I get this exception:
11-19 11:17:16.536: DEBUG/DocsAPI(32195): error: <errors xmlns='http://schemas.google.com/g/2005'>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): <error>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): <domain>GData</domain>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): <code>noPostConcurrency</code>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): <internalReason>POST method does not support concurrency</internalReason>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): </error>
11-19 11:17:16.536: DEBUG/DocsAPI(32195): </errors>
11-19 11:17:16.536: WARN/System.err(32195): com.google.api.client.http.HttpResponseException: 501 Not Implemented
11-19 11:17:16.540: WARN/System.err(32195): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:209)
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要更新现有文档,您应该使用 PUT 命令:更新文档
For updating an existing document you should use PUT command: Updating documents
您首先需要查询该文件。在响应中,您想要在链接列表中查找名称为“edit-media”的元素。然后您将邮件发送到该地址。
下面的代码可以从 google-client api 的网站 http://code.google.com/p/google-api-java-client/wiki/GoogleAPIs
You first need to query for the file. In the response you want to look for an element among the list of links whose name is "edit-media". You then post to that address.
The code below can be added to Google's sample project docs-v3-atom-oauth-sample from google-client api's website http://code.google.com/p/google-api-java-client/wiki/GoogleAPIs