2013-02-01 138 views
3

我需要我的android应用程序总是连接到相同的谷歌电子表格以获取单元格数据(我将在未来每天更改,以便应用程序可以在不使用服务器的情况下获取更新的数据)。如何直接从谷歌电子表格获取数据?

在文档https://developers.google.com/google-apps/spreadsheets/?hl=it#ListFeeds它的表现如何认证等,但我需要的是什么使用像直链接连接到一个公共的电子表格:

https://docs.google.com/spreadsheet/ccc?key=xxx ....

这可能吗?

+0

有可能!我为此编码了一个js库,但不是为了android(没有检查)。 [Gsheet2json](http://rumal.github.com/Gsheet2json/)可能是库或代码可能会帮助你将它移植到android – udnisap

回答

3

是可能的,例如一些可詹姆斯·穆尔的http://blog.restphone.com/2011/05/very-simple-google-spreadsheet-code.html

记住,你需要手动添加在电子表格中的代码“文件 - >发布到Web”

package com.banshee; 

    import java.io.IOException; 
    import java.net.URL; 

    import com.google.gdata.client.spreadsheet.SpreadsheetService; 
    import com.google.gdata.data.spreadsheet.CustomElementCollection; 
    import com.google.gdata.data.spreadsheet.ListEntry; 
    import com.google.gdata.data.spreadsheet.ListFeed; 
    import com.google.gdata.util.ServiceException; 

    public class SpreadsheetSucker { 
     public static void main(String[] args) { 
     SpreadsheetService service = new SpreadsheetService("com.banshee"); 
     try { 
      // Notice that the url ends 
      // with default/public/values. 
      // That wasn't obvious (at least to me) 
      // from the documentation. 
      String urlString = "https://spreadsheets.google.com/feeds/list/0AsaDhyyXNaFSdDJ2VUxtVGVWN1Yza1loU1RPVVU3OFE/default/public/values"; 

      // turn the string into a URL 
      URL url = new URL(urlString); 

      // You could substitute a cell feed here in place of 
      // the list feed 
      ListFeed feed = service.getFeed(url, ListFeed.class); 

      for (ListEntry entry : feed.getEntries()) { 
      CustomElementCollection elements = entry.getCustomElements(); 
      String name = elements.getValue("name"); 
      System.out.println(name); 
      String number = elements.getValue("Number"); 
      System.out.println(number); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (ServiceException e) { 
      e.printStackTrace(); 
     } 

     } 
    } 
+0

这对我有效!但我们必须记住,我们必须在清单文件添加权限中添加guava.jar(在我的proj中为guava-18.jar),最后将threadpolicy设置为PermitAll()。那是所有:) –

+0

不再支持,因为谷歌强制OAuth2.0从表格访问数据。通过将表格数据作为JSON访问并在您的应用结束时解析它,仍然有一种解决方法。使用此URL 访问JSON:https://spreadsheets.google.com/tq?key= 例如为:https://spreadsheets.google.com/tq?key=1tJ64Y8hje0ui4ap9U33h3KWwpxT_-JuVMSZzxD2Er8k –