1

ドキュメントとフォルダの作成と削除を行うために、ほぼ完全なAPIを手に入れました。しかし、私は文書を更新するのに失敗します。 gdataを使用すると非常に簡単でしたが、このコードはすべてのアンドロイドデバイスで動作する必要があるため、google api javaクライアントを使用する必要があります。ここで私は更新をテストする方法は次のとおりです。google api java clientを使ってgoogle docsドキュメントを更新する

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

からGoogleのサンプルプロジェクトのドキュメント-V3-原子OAuthのサンプルに加えることができ、感謝! – pgsandstrom

+0

ああ、私は10%をターゲットにしていました。 Bummer;) –

+1

あなたは私を3%幸せにしました。だからあなたは今あなたの目標を超越したと思います。 ;) –

1

あなたのために照会する最初の必要性ファイル。レスポンスでは、名前が "edit-media"であるリンクのリストの中から要素を探す必要があります。その後、その住所に投稿します。あなたは私だけ約8%がより幸せに作ら

次のコードは、グーグル・クライアント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(); 
    } 
} 
関連する問題