Scorpion Scorpion - 5 months ago 32
Android Question

How to sync Google Spreadsheet in Android app and read rows from it

I have one Spreadsheet file in my Google Drive which i want to synch from Android Application. After successful synch I have some search criteria in my apps, I want results based on this criteria from the file which I synch. I am doing this first time and not getting good resources to start with. Can anybody please kindly help.

1) How to synch Google Drive file in Android App?

2) How to retrieve particular data from the Spreadsheet which i have synch?

Note - I am also confused between Google Docs API & Google Drive SDK.

What to use in my app?

I have only one file to synch every time, not all the files. And the file is open to all with a private key.

I have implemented some code but its not working somehow. Below is my spreadsheet public url which i want to synch in my android app.

I am following steps from here.

My Test Spreadsheet URL

try {
String USERNAME = "xxxxx";
String PASSWORD = "xxxxx";

SpreadsheetService service =
new SpreadsheetService("Testing");
service.setUserCredentials(USERNAME, PASSWORD);
// TODO: Authorize the service object for a specific user (see other sections)

// Define the URL to request. This should never change.
URL SPREADSHEET_FEED_URL = new URL(
"https://spreadsheets.google.com/feeds/spreadsheets/private/full");

// Make a request to the API and get all spreadsheets.
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();

if (spreadsheets.size() == 0) {
// TODO: There were no spreadsheets, act accordingly.
}

// TODO: Choose a spreadsheet more intelligently based on your
// app's needs.
SpreadsheetEntry spreadsheet = spreadsheets.get(0);
System.out.println(spreadsheet.getTitle().getPlainText());

// Make a request to the API to fetch information about all
// worksheets in the spreadsheet.
List<WorksheetEntry> worksheets = spreadsheet.getWorksheets();

// Iterate through each worksheet in the spreadsheet.
for (WorksheetEntry worksheet : worksheets) {
// Get the worksheet's title, row count, and column count.
String title = worksheet.getTitle().getPlainText();
int rowCount = worksheet.getRowCount();
int colCount = worksheet.getColCount();

// Print the fetched information to the screen for this worksheet.
System.out.println("\t" + title + "- rows:" + rowCount + " cols: " + colCount);
}
} catch (Exception e) {
e.printStackTrace();
}


All suggestions are welcome.

Answer

I got it. I have not added the permission and that is why it is giving me error. We need to add below permission for authorization.

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