Google文档列表API - 创建新文档并将内容更新到其中

时间:2011-07-22 09:20:52

标签: api list documents

我正在尝试创建一个新文档,并将内容更新到其中。在过去的两天里,我有时成功地更新了文档的标题,但从未更新内容。我收到了这个错误:

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("myemail...@gmail.com", "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 个答案:

答案 0 :(得分:1)

Google文档使用etags跟踪文件的版本信息。 当您收到此错误时,表示文件内容/元内容已更改。一种可能的解决方法是再次获取资源。

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