Search code examples
gdata-api

Issue in reading google text document


I could get the handle to the google text doc i needed. I am now stuck at how to read the contents. My code looks like:

            GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();  
            oauthParameters.setOAuthConsumerKey(Constants.CONSUMER_KEY);  
            oauthParameters.setOAuthConsumerSecret(Constants.CONSUMER_SECRET);  
            oauthParameters.setOAuthToken(Constants.ACCESS_TOKEN); 
            oauthParameters.setOAuthTokenSecret(Constants.ACCESS_TOKEN_SECRET);  
            DocsService client = new DocsService("sakshum-YourAppName-v1");  
            client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());  
            URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full/");  
            DocumentQuery dquery = new DocumentQuery(feedUrl);
            dquery.setTitleQuery("blood_donor_verification_template_dev");
            dquery.setTitleExact(true);
            dquery.setMaxResults(10);
            DocumentListFeed resultFeed = client.getFeed(dquery, DocumentListFeed.class);
            System.out.println("feed size:" + resultFeed.getEntries().size());
            String emailBody = "";
            for (DocumentListEntry entry : resultFeed.getEntries()) {  

                System.out.println(entry.getPlainTextContent()); 
                emailBody = entry.getPlainTextContent();
            }  

Plz note that entry.getPlainTextContent() does not work and throws object not TextContent type exception


Solution

  • finally i solved it as:

    for (DocumentListEntry entry : resultFeed.getEntries()) {  
                    String docId = entry.getDocId();
                    String docType = entry.getType();
                    URL exportUrl =
                                  new URL("https://docs.google.com/feeds/download/" + docType
                                      + "s/Export?docID=" + docId + "&exportFormat=html");
                    MediaContent mc = new MediaContent();
                    mc.setUri(exportUrl.toString());
    
                    MediaSource ms = client.getMedia(mc);
                    InputStream inStream = null;
                    try {
                        inStream = ms.getInputStream();
                        int c;
                        while ((c = inStream.read()) != -1) {
                            emailBody.append((char)c);
                        }
                      } finally {
                        if (inStream != null) {
                          inStream.close();
                        }
                      }                 
                }