How do I connect Android apps with Google Sheets spreadsheets?

user1680435 picture user1680435 · Sep 18, 2012 · Viewed 25.2k times · Source

I'm trying to do an Android app that needs to work with Google spreadsheet API. I'm new in this, so I'm starting with the version 3 of the api: https://developers.google.com/google-apps/spreadsheets/

I followed all the steps, downloaded all the jar files to lib subfolder in my project folder and then I added to the build path in Eclipse as usual. So although there is no Java example to perform Oauth 2.0, I just tried to declare:

SpreadsheetService service = new SpreadsheetService("v1");

but when I emulate this simple line it gives me an error:

java.lang.NoClassDefFoundError: com.google.gdata.client.spreadsheet.SpreadsheetService

I'm using all the jars included in the documentation and I have the import:

import com.google.gdata.client.spreadsheet.SpreadsheetService;

but I am totally lost. I dont know what else to do just to start, connect to Google APIs and work with the spreadsheets.

Answer

Scorpion picture Scorpion · Oct 16, 2012

Sample code for you without OAuth 2.0. But its recommended to perform OAuth as its good for the security purpose. You also have to add below permissions.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCOUNT_MANAGER"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Sample Code:-

try {
    SpreadsheetEntry spreadsheet;
    service = new SpreadsheetService("Spreadsheet");
    service.setProtocolVersion(SpreadsheetService.Versions.V3);
    service.setUserCredentials("username", "password");//permission required to add in Manifest
    URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
    feed = service.getFeed(metafeedUrl, SpreadsheetFeed.class);

    List<SpreadsheetEntry> spreadsheets = feed.getEntries();
    if (spreadsheets.size() > 0) {
        spreadsheet = spreadsheets.get(i);//Get your Spreadsheet
   }
} catch (Exception e) {
    e.printStackTrace();
}