Google Spreadsheet使用SpreadsheetService类为android

时间:2014-01-21 10:33:38

标签: android google-sheets google-spreadsheet-api

尝试使用SpreadsheetService类访问Google电子表格时,我正在获取ExceptionInInitializerError

以下是我的代码:

我已将以下jar文件包含在文件

gdata-client-1.0.jar
gdata-client-meta-1.0.jar
gdata-core-1.0.jar
gdata-spreadsheet-3.0.jar
gdata-spreadsheet-meta-3.0.jar
google-api-client-1.12.0-beta.jar
google-api-client-android-1.12.0-beta.jar
google-http-client-1.12.0-beta.jar
google-http-client-android-1.12.0-beta.jar
google-oauth-client-1.12.0-beta.jar
gson-2.1.jar
guava-13.0.1.jar

jackson-core-asl-1.9.9.jar
jsr305-1.3.9.jar
protobuf-java-2.4.1.jar

代码如下:

SpreadsheetService service = new SpreadsheetService("ContactsApp");// Exception occured here
service.setProtocolVersion(SpreadsheetService.Versions.V3);
try {
service.setUserCredentials("abcd@gmail.com","pwd1234");
    } catch (Exception e) {
e.printStackTrace();
}
ArrayList<ContactsModel> list = new ArrayList<ContactsModel>();
try {

String url=
 "https://spreadsheets.google.com/feeds/list/0AsaDhyyXNaFSdDJ2VUxtVGVWN1Yza1loU1RPVVU3OFE/default/public/values";

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

// You could substitute a cell feed here in place of
// the list feed

ListFeed feed = service.getFeed(urlString, ListFeed.class);

for (ListEntry entry : feed.getEntries()) {
ContactsModel contact = new ContactsModel();
CustomElementCollection elements = entry.getCustomElements();
contact.EmpID = elements.getValue("Emp Id");
contact.Name = elements.getValue("Name");
contact.personalEmail = elements.getValue("personal email");
contact.workEmail = elements.getValue("work email");
contact.emergencyContact = elements.getValue("Emergency Contact");
contact.relation = elements.getValue("Relation");

list.add(contact);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}

1 个答案:

答案 0 :(得分:0)

Google ListFeed演示

http://code.metager.de/source/xref/google/gdata-java-client/java/sample/spreadsheet/list/ListDemo.java

该演示是基本的,重点,我认为包括所有ListFeed功能。

URL

但是看着它,我认为你的网址缺少工作表ID。

/**********************
 * Standard google urls for spreadsheet service 
 * copied direct from google code
 *********************/
public URL getSpreadsheetsUrl() throws MalformedURLException {
    return new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full");
}

public URL getSpreadsheetUrl(String key) throws MalformedURLException {
    return new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full/"
                    + key);
}

public URL getWorksheetsUrl(String key) throws MalformedURLException {
    return new URL("https://spreadsheets.google.com/feeds/worksheets/" + key
            + "/private/full");
}

public URL getWorksheetUrl(String key, String worksheetId)
        throws MalformedURLException {
    return new URL("https://spreadsheets.google.com/feeds/worksheets/" + key
            + "/private/full/" + worksheetId);
}

public URL getCellsUrl(String key, String worksheetId)
        throws MalformedURLException {
    return new URL("https://spreadsheets.google.com/feeds/cells/" + key
            + "/" + worksheetId + "/private/full");
}

public URL getCellUrl(String key, String worksheetId, String cellId)
        throws MalformedURLException {
    return new URL("https://spreadsheets.google.com/feeds/cells/" + key
            + "/" + worksheetId + "/private/full/" + cellId);
}


public URL getTableUrl(String key, String worksheetId, String rowNumber)
        throws MalformedURLException {
    return new URL("https://spreadsheets.google.com/feeds/" + key
            + "/records/" + rowNumber);
}