2013-02-01 99 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 –