【问题标题】:Updating google docs document with google api java client使用 google api java 客户端更新 google docs 文档
【发布时间】:2010-11-19 10:25:34
【问题描述】:

我已经获得了几乎完整的 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: *”-header,我会得到这个异常:

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)
...

【问题讨论】:

    标签: java google-api google-docs google-docs-api google-api-java-client


    【解决方案1】:

    要更新现有文档,您应该使用 PUT 命令:Updating documents

    【讨论】:

    • 哦,我的目标是 10%。无赖 ;)
    • 你让我快乐了 3%,所以我猜你现在已经超出了你的目标。 ;)
    【解决方案2】:

    您首先需要查询文件。在响应中,您想在名称为“edit-media”的链接列表中查找一个元素。然后你发到那个地址。

    下面的代码可以从google-client api的网站http://code.google.com/p/google-api-java-client/wiki/GoogleAPIs添加到Google的示例项目docs-v3-atom-oauth-sample中

    private String queryRegistryforEditId() {
        String str ="https://docs.google.com/feeds/default/private/full?title=" + URL_FRIENDLY_QUERY_PHRASE;
        DocsUrl url = new DocsUrl(str);
    
        DocumentListFeed feed;
        try {
            feed = DocumentListFeed.executeGet(transport, url);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    
        //display(feed);
        String ans = null;
        //LIST OF FILES MATCHING QUERY
        for (DocumentListEntry doc : feed.docs) {
            //doc.content.src has url to download file
            //I added src to content class that comes from the sameple code
            Map<String, String> data = retriveDocUsingId(doc.content.src);
    
            List<Link> lik = doc.links;
            for (Link i : lik) {
                //look for "edit-media" to get url to post edits to file
                if (i.rel.equals("edit-media")) {
                    ans = i.href;
                    System.out.println(i.href);
                }
            }
            //System.out.println(" doc.title: " + doc.title + " doc.id " + doc.id);
        }
        return ans;
    }  
    
    private void updateDocumentText(String edit) {
        HttpRequest request = transport.buildPutRequest();
        request.url = new GoogleUrl(edit);
    
        GoogleHeaders headers = (GoogleHeaders)transport.defaultHeaders;
        headers.contentType = "text/plain";
        headers.gdataVersion = "3";
        headers.slug = "examplefile";
        headers.ifMatch = "*";      
        request.headers = headers;
    
        AtomParser parser = new AtomParser();
        parser.namespaceDictionary = Namespace.DICTIONARY;
        transport.addParser(parser);
        File file = new File ("/newfilepath/test233.txt");
    
        InputStreamContent bContent = new InputStreamContent();
        bContent.type = "text/plain";
        request.content = bContent;
    
        try {
            bContent.setFileInput(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    
        com.google.api.client.http.HttpResponse res2;
        try {
            res2 = request.execute();
            System.out.println(res2.parseAsString());
        } catch (HttpResponseException e) {
            try {
                System.out.println(e.response.parseAsString());
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }   
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多