Sunday, July 28, 2013

Reading Google Spreadsheets from an Android App

Well, I've done it. It took me two serious days of wading through the mess that is the gdata APIs (myths and outdated versions abound) and the OAuth2 APIs (same caveats apply) and I was finally able to read the list of my own personal spreadsheets in Google Docs, and print their names out from an Android application.

It was very important to me to be able to use Android's own build-in account mechanism, which took a fair bit of time to figure out, and then on top of that the build/class-path requirements for the Google Spreadsheets API is completely messed up.

But anyway, here's a short description of how I did it, notes, and the full code for the main activity.

1 - I am using a Nexus 4, so I set up my app to use the latest APIs possible (Android 4.2.2 on my system).

2 - The list of required JARs is crazy, and quite picky. Here's the screenshots of the ones I am using:

3 - I had to add the following permissions to my manifest file:

4 - And finally, here is the code itself to run the main activity. The basic application structure was just the one that is created by default when you create a new Android Application. The basic steps for listing the Spreadsheets I got from the Spreadsheets API Tutorial. I can't really remember where I learned the OAuth2.0 steps, but needless to say, I cobbled it together from several online sources.




18 comments:

  1. Code from my project is now open-sourced as well:
    https://code.google.com/p/nolacoaster/source/browse/#svn%2Ftrunk%2FMegabudget

    ReplyDelete
    Replies
    1. This is awesome, thanks!.... I tried to add internet permission and got this , please help..."
      The
      tag requests a {@link #AndroidManifestPermission } that the containing package must be granted in order for it to operate correctly. "

      Delete
    2. Hi chprofits,
      Can you try editing the AndroidManifest.xml file directly? Here's what mine looks like:

      https://code.google.com/p/nolacoaster/source/browse/trunk/Megabudget/AndroidManifest.xml#11

      I've linked to the line that adds the Internet permission.

      Delete
  2. Awesome Aaron. Glad to hear it helped.

    ReplyDelete
  3. This is awesome, thanks! Any chance you could share a template of the google spreadsheet that you are using in conjunction with this?

    ReplyDelete
    Replies
    1. Thanks!

      Sure! Let me remove my budget information and then share it. I'll try to post one very soon.

      Delete
    2. I can't tell you thank you enough. I use a google spreadsheet for an expense tracker for my wife and I, and I've really been wanting to build an app to allow us to easily post data to it. However, I'm a beginner developer and I quickly realized that accessing the Spreadsheets API was way too complicated for someone of my experience level. But with your app to use as an example, I'm confident I'll be able to adapt your code for my needs.

      If you have a donate link, I'll be happy to send you a small token of my appreciation.

      Delete
    3. Okay, I made a template file and then saved it from Google Docs as a CSV file. You should be able to upload this back to Google Docs. It's posted to the source depot:

      https://code.google.com/p/nolacoaster/source/browse/trunk/Megabudget/BUDGET%20TEMPLATE%20-%20Sheet1.csv

      Delete
    4. Hey no problem at all. If you have any suggestions, or any questions that I might be able to help you with, just let me know. $$$ is not needed at all, just the thought that the hours I spent trying to figure this out might not have been in vain.

      ;-)

      Delete
    5. I've got my app working, thanks to you! Just wanted to let you know again how helpful your source code was. I'm not sure I would have ever figured out the Oauth stuff on my own.

      Delete
  4. Thank you very much. Your code helped me understand many things.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. Hi Sir.I'm a newbie and want read values (as json) from my googleform which is published to web. I've tried you code but I'm getting "SystemUiHider cannot be resolved to a type" error. Do I need to use any API for that. Please make a reply...

    ReplyDelete
    Replies
    1. You can query data as JSON from Sheets using Google Vizualization API, and the requests are made via HTTP so no need to include JARs in your project!

      A simple example would be: https://spreadsheets.google.com/tq?key=

      Source: http://www.telerik.com/blogs/google-spreadsheet-as-data-source-android

      Delete
    2. Excellent, thanks. I haven't been able to actually try that out myself, but sounds promising.

      Delete