api
  • list
  • documents
  • 2011-07-22 101 views 0 likes 
    0

    我想創建一個新文檔,並更新內容到它。在過去的兩天中,我有時成功地更新了文檔的標題,但沒有更新內容。我得到這個錯誤:谷歌文檔列表API - 創建一個新文檔,並更新它的內容

    com.google.gdata.util.PreconditionFailedException: Mismatch: etags = ["GEIJRhlABSt7ImBr"], version = [gqdjfe36] 
    <errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>etagsMismatch</code><internalReason>Mismatch: etags = ["GEIJRhlABSt7ImBr"], version = [gqdjfe36]</internalReason></error></errors> 
    
        at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:606) 
        at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:563) 
        at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:552) 
        at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:530) 
        at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535) 
        at com.google.gdata.client.Service.update(Service.java:1563) 
        at com.google.gdata.client.Service.update(Service.java:1530) 
        at com.google.gdata.client.GoogleService.update(GoogleService.java:583) 
        at com.google.gdata.client.media.MediaService.update(MediaService.java:484) 
        at com.google.gdata.data.BaseEntry.update(BaseEntry.java:639) 
        at GoogleDocuments.main(GoogleDocuments.java:51) 
    Exception in thread "main" java.lang.NullPointerException 
        at GoogleDocuments.main(GoogleDocuments.java:60) 
    

    今天早上,我試了相同的代碼,我成功地更新了文檔的標題和內容。這實在是不可預測的。 我花了幾個小時,我仍然不明白是什麼錯。

    謝謝你幫我

    大衛

    Java代碼:

    import java.io.IOException; 
    import java.net.URL; 
    
    import com.google.gdata.client.docs.DocsService; 
    import com.google.gdata.data.docs.DocumentListEntry; 
    import com.google.gdata.util.AuthenticationException; 
    import com.google.gdata.util.ServiceException; 
    
    
    import com.google.gdata.data.PlainTextConstruct; 
    import com.google.gdata.data.docs.*; 
    import com.google.gdata.data.media.MediaByteArraySource; 
    
    public class GoogleDocuments { 
    
        /** 
        * @param args 
        */ 
        public static void main(String[] args) { 
    
         DocsService client = new DocsService("uop-test-v1"); 
    
         try { 
          client.setUserCredentials("[email protected]", "mypassword"); 
         } catch (AuthenticationException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
    
         DocumentListEntry createdEntry = null; 
    
         // Create an empty word processor document 
         try { 
          createdEntry = createNewDocument("NewDocTitle", client); 
         } catch (IOException e2) { 
          // TODO Auto-generated catch block 
          e2.printStackTrace(); 
         } catch (ServiceException e2) { 
          // TODO Auto-generated catch block 
          e2.printStackTrace(); 
        } 
    
         System.out.println("Document now online @ :" + createdEntry.getHtmlLink().getHref()); 
         System.out.println("Title of the document :" + createdEntry.getTitle().getPlainText()); 
    
         createdEntry.setTitle(new PlainTextConstruct("NewTitle")); 
    
         DocumentListEntry updatedEntry = null; 
    
         try { 
          updatedEntry = createdEntry.update(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (ServiceException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
    
         System.out.println("New title of the document :" +updatedEntry.getTitle().getPlainText()); 
    
         updatedEntry.setMediaSource(new MediaByteArraySource("updated content".getBytes(), "text/plain")); 
    
         try { 
          updatedEntry.updateMedia(true); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (ServiceException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
    
         System.out.println("Document updated!"); 
        } 
    
        static public DocumentListEntry createNewDocument(String title, DocsService client) throws IOException, ServiceException { 
         DocumentListEntry newEntry = null; 
         newEntry = new DocumentEntry(); 
         newEntry.setTitle(new PlainTextConstruct(title)); 
         return client.insert(new URL("http://docs.google.com/feeds/default/private/full"), newEntry); 
        } 
    } 
    

    回答

    1

    谷歌文檔跟蹤版本使用的ETag文件的信息。 當您收到此錯誤時,表示文件內容/元內容已更改。一種可能的解決方法是再次獲取資源。

    看一看http://www.google.com/support/forum/p/apps-apis/thread?tid=2f5f76354541137b&hl=en

    +0

    證實,我當我設置工作表的標題,然後試圖插入行得到這個錯誤。看起來您需要在文檔操作之間進行全新的複製。 – bsautner

    +0

    @bsautner,你有一個如何做到這一點的例子? –

    +0

    對不起,我放棄使用gdoc作爲我的應用程序的輸出,因爲我無法保持穩定。如果內存服務 - 如果您有編輯文檔的代碼(例如設置文件名或編輯單元格),則必須分別進行每項操作。打開文件,設置標題並關閉它。重新打開並插入單元格並關閉等。希望有所幫助。 – bsautner

    相關問題