使用google api java客户端更新google文档

使用google api java客户端更新google文档,java,google-api,google-docs,google-docs-api,google-api-java-client,Java,Google Api,Google Docs,Google Docs Api,Google Api Java Client,我已经有了一个几乎完整的API,可以创建和删除文档和文件夹。但我在更新文件方面失败了。使用gdata时非常简单,但由于此代码必须在所有android设备上运行,因此我必须使用GoogleAPI java客户端。以下是我测试更新的方法: public void updateTest() throws IOException { InputStreamContent isContent = new InputStreamContent(); isContent.inputStream

我已经有了一个几乎完整的API,可以创建和删除文档和文件夹。但我在更新文件方面失败了。使用gdata时非常简单,但由于此代码必须在所有android设备上运行,因此我必须使用GoogleAPI 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)
...

要更新现有文档,应使用PUT命令:

要更新现有文档,应使用PUT命令:

首先需要查询文件。在响应中,您希望在链接列表中查找名为“edit media”的元素。然后你把邮件寄到那个地址

下面的代码可以从Google客户端api的网站添加到Google的示例项目docs-v3-atom-oauth-sample中

私有字符串queryRegistryforEditId(){
字符串str=”https://docs.google.com/feeds/default/private/full?title=“+URL\u友好的\u查询\u短语;
DocsUrl url=新的DocsUrl(str);
文档列表提要;
试一试{
feed=DocumentListFeed.executeGet(传输,url);
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
//显示(馈送);
字符串ans=null;
//匹配查询的文件列表
for(DocumentListEntry文档:feed.docs){
//doc.content.src有下载文件的url
//我将src添加到来自sameple代码的content类中
映射数据=检索文档ID(doc.content.src);
List lik=doc.links;
用于(链接i:lik){
//查找“编辑媒体”以获取url以将编辑内容发布到文件
如果(i.rel.equals(“编辑介质”)){
ans=i.href;
System.out.println(i.href);
}
}
//System.out.println(“doc.title:+doc.title+“doc.id”+doc.id”);
}
返回ans;
}  
私有void updateDocumentText(字符串编辑){
HttpRequest请求=transport.buildPutRequest();
request.url=新谷歌url(编辑);
GoogleHeaders=(GoogleHeaders)transport.defaultHeaders;
headers.contentType=“text/plain”;
headers.gdataVersion=“3”;
headers.slug=“examplefile”;
headers.ifMatch=“*”;
request.headers=头;
AtomParser parser=新的AtomParser();
parser.namespaceDictionary=Namespace.DICTIONARY;
addParser(解析器);
File File=新文件(“/newfilepath/test233.txt”);
InputStreamContent bContent=新的InputStreamContent();
bContent.type=“text/plain”;
request.content=b内容;
试一试{
bContent.setFileInput(文件);
}catch(filenotfounde异常){
e、 printStackTrace();
}
com.google.api.client.http.HttpResponse res2;
试一试{
res2=request.execute();
System.out.println(res2.parseAsString());
}捕获(HttpResponseException e){
试一试{
System.out.println(例如response.parseAsString());
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}   
}捕获(例外e){
e、 printStackTrace();
}
}

首先需要查询文件。在响应中,您希望在链接列表中查找名为“edit media”的元素。然后你把邮件寄到那个地址

下面的代码可以从Google客户端api的网站添加到Google的示例项目docs-v3-atom-oauth-sample中

私有字符串queryRegistryforEditId(){
字符串str=”https://docs.google.com/feeds/default/private/full?title=“+URL\u友好的\u查询\u短语;
DocsUrl url=新的DocsUrl(str);
文档列表提要;
试一试{
feed=DocumentListFeed.executeGet(传输,url);
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
//显示(馈送);
字符串ans=null;
//匹配查询的文件列表
for(DocumentListEntry文档:feed.docs){
//doc.content.src有下载文件的url
//我将src添加到来自sameple代码的content类中
映射数据=检索文档ID(doc.content.src);
List lik=doc.links;
用于(链接i:lik){
//查找“编辑媒体”以获取url以将编辑内容发布到文件
如果(i.rel.equals(“编辑介质”)){
ans=i.href;
System.out.println(i.href);
}
}
//System.out.println(“doc.title:+doc.title+“doc.id”+doc.id”);
}
返回ans;
}  
私有void updateDocumentText(字符串编辑){
HttpRequest请求=transport.buildPutRequest();
request.url=新谷歌url(编辑);
GoogleHeaders=(GoogleHeaders)transport.defaultHeaders;
headers.contentType=“text/plain”;
headers.gdataVersion=“3”;
headers.slug=“examplefile”;
headers.ifMatch=“*”;
request.headers=头;
AtomParser parser=新的AtomParser();
parser.namespaceDictionary=Namespace.DICTIONARY;
addParser(解析器);
File File=新文件(“/newfilepath/test233.txt”);
InputStreamContent bContent=新的InputStreamContent();
bContent.type=“text/plain”;
request.content=b内容;
试一试{
bContent.setFileInput(文件);
}catch(filenotfounde异常){
e、 printStackTrace();
}
com.google.api.client.http.HttpResponse res2;
试一试{
res2=request.execute();
System.out.println(res2.parseAsString());
}捕获(HttpResponseException e){
试一试{
System.out.println(例如response.parseAsString());
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}   
}捕获(例外e){
e、 printStackTrace();
}
}

哦,我的目标是10%。糟糕的;)你让我开心了3%
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();
    }
}