1

我已經得到了幾乎完整的API工作,與創建和刪除文檔和文件夾。但我無法更新文檔。使用gdata時非常簡單,但由於此代碼必須適用於所有Android設備,因此我必須使用google api java客戶端。這裏是我測試更新的方法:更新谷歌文檔與谷歌API api客戶端

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; 
    } 
} 

什麼情況是,我只需要創建一個新的文件(與給定的內容,創建一個新的文件運行完美)。如果我添加「如果 - 匹配:*」 - 頭,我得到這個異常:

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

回答

3

用於更新現有的文件,你應該使用PUT命令:Updating documents

+2

你只是讓我約8%更開心,謝謝! – pgsandstrom 2010-11-19 14:07:05

+0

哦,我的目標是10%。 Bummer;) – 2010-11-19 14:18:45

+1

你讓我快樂3%,所以我猜你現在已經超出了你的目標。 ;) – 2011-06-02 11:47:15

1

首先,您需要查詢的文件。在響應中,您希望查找名稱爲「edit-media」的鏈接列表中的某個元素。您然後發佈到該地址。

下面可以添加到谷歌的樣本項目文檔-V3-原子OAuth的樣品從谷歌的客戶端API的網站代碼http://code.google.com/p/google-api-java-client/wiki/GoogleAPIs

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(); 
    } 
} 
相關問題