Building model classes using @Key annotation for the Spreadsheet list feed (probably stupid question)

79 views
Skip to first unread message

Joel Edström

unread,
May 13, 2011, 8:09:04 AM5/13/11
to google-api-java-client
After watching the Google IO talk I've started playing with the API on
Android. The goal was to write a simple spreadsheet "library" to sit
between google-api-java-client and the android application.

I started with adding support for finding and opening a Spreadsheet
list feed by name, and extracting the row data, but got stuck on the
last step:

First I've successfully managed to build model classes for the
Spreadsheet feed (which list all the users spreadsheets), and the
Worksheet feed (which lists all worksheets in a spreadsheet).

But then as I said I got stuck on the last step: building the model
class for the List feed (which contains the content of each
spreadsheet row). There is an example of what the XML looks like for
the list feed here: http://code.google.com/apis/spreadsheets/data/3.0/developers_guide.html#ListFeeds

Basically my question is: How do I access the actual cell content and
names(the tags in the gsx namespace) using the @Key notation ?

(of course I cant put static fields in the model because the amount of
columns and columnnames vary from spreadsheet to spreadsheet)

This is how my model classes currently look, except the cells field,
which I've lots of different approaches for, but I just cant get it to
work. (everything else works, i can successfully get the row "id" for
example)

public static class ListFeed {
@Key("entry") public List<String> entries;
}

public static class ListEntry {
@Key public String id;
@Key("gsx:*") public Map<String, String> cells; // just to
illustrate what i want
}

Is there anyway to get something like this to work?

Thanks
Joel Edström

Joel Edström

unread,
May 13, 2011, 9:15:00 PM5/13/11
to google-api-java-client
I extracted out the "library" from my app, and wrote a sample android
app that uses it. It requires you to have at least one spreadsheet in
your Docs account, or it will crash), and uses AccountManager as
authentication.
Maybe this code could be useful for someone else who is looking for
a spreadsheets example as well, but the code is kind of messy - at
first I thought I was writing it in a smart way with the
FeedResponse<T>, but I'm not sure now :)

For each row in your "first" (whatever that happens to be) spreadsheet
it will log the row ID (the <id> tag), filter for "E/
SpreadsheetTestActivity" in LogCat to get the output.

Uploaded the sample here: github.com/joeledstrom/SpreadsheetTest

It's an working eclipse project, and as a convenience I included just
the JARs that are necessary from: google-api-java-client-1.4.1-
beta.zip.


The non working model class is the WorksheetRow.ListEntry class in
WorksheetRow.java ( http://goo.gl/lV8nZ ), I need to be able to get
those <gsx:COLUMN-NAME> elements into a List or Map using the @Key
notation in someway. If anyone has any ideas it would be very helpful.

Thanks
Joel Edström

Yaniv Inbar (יניב ענבר)

unread,
May 13, 2011, 10:31:31 PM5/13/11
to google-api-...@googlegroups.com
Thanks for sharing your code.  This is a good question.

I think the easiest option is to declare ListEntry like this:

public static class ListEntry extends GenericXml {
   @Key public String id;
}

You can iterate over the values for example:

for (Map.Entry<String, Object> cell : listEntry.entrySet()) {
  String key = cell.getKey(); // "gsx:..."
  Object value = cell.getValue();
}

Let me know if that works for you.

Yaniv Inbar
Senior Software Engineer
Google Inc.

Joel Edström

unread,
May 14, 2011, 7:41:45 AM5/14/11
to google-api-java-client
Thanks a lot :)

Managed to get it working, although probably not an optimal solution,
but it works for now:

Map<String, String> cells = new HashMap<String,String>();

for (Map.Entry<String, Object> e : entry.entrySet()) {
String key = e.getKey();
if (key.startsWith("gsx:")) {
String value = ((Map)
((List)e.getValue()).get(0)).get("text()").toString();
cells.put(key.substring(4), value);
}
}



Thanks again.
Joel Edström

On May 14, 4:31 am, Yaniv Inbar (יניב ענבר) <yan...@google.com> wrote:
> Thanks for sharing your code.  This is a good question.
>
> I think the easiest option is to declare ListEntry like this:
>
> public static class ListEntry extends GenericXml {
>    @Key public String id;
>
> }
>
> You can iterate over the values for example:
>
> for (Map.Entry<String, Object> cell : listEntry.entrySet()) {
>   String key = cell.getKey(); // "gsx:..."
>   Object value = cell.getValue();
>
> }
>
> Let me know if that works for you.
>
> Yaniv Inbar
> Senior Software Engineer
> Google Inc.
>
> On Fri, May 13, 2011 at 6:15 PM, Joel Edström <joel.edst...@gmail.com>wrote:
>
>
>
>
>
>
>
> > I extracted out the "library" from my app, and wrote a sample android
> > app that uses it. It requires you to have at least one spreadsheet in
> > your Docs account, or it will crash), and uses AccountManager as
> > authentication.
> >   Maybe this code could be useful for someone else who is looking for
> > a spreadsheets example as well, but the code is kind of messy - at
> > first I thought I was writing it in a smart way with the
> > FeedResponse<T>, but I'm not sure now :)
>
> > For each row in your "first" (whatever that happens to be) spreadsheet
> > it will log the row ID (the <id> tag), filter for "E/
> > SpreadsheetTestActivity" in LogCat to get the output.
>
> > Uploaded the sample here: github.com/joeledstrom/SpreadsheetTest
>
> > It's an working eclipse project, and as a convenience I included just
> > the JARs that are necessary from: google-api-java-client-1.4.1-
> > beta.zip.
>
> > The non working model class is the WorksheetRow.ListEntry class in
> > WorksheetRow.java (http://goo.gl/lV8nZ), I need to be able to get
Reply all
Reply to author
Forward
0 new messages