user605331
user605331

Reputation: 3788

Android: Upload spreadsheet to Google Drive and read worksheet contents using http client

I have an Android app where I would like to create a new spreadsheet in Google Drive and read the contents back out later (where the user will have added data to it using the Google Drive web client).

As I understand it I cannot create a spreadsheet with the Spreadsheets API so I need to use the Drive API. Getting either of them to work has been a fight with missing classes and copying different jar files (some of which seem to have been in alpha status for a while now) into my lib directory. I've seen some places saying to just click some button in Eclipse, but I'm using IntelliJ, although clicking some button doesn't seem like a good way to go about this anyway.

For what I'm trying to do, I don't need much in the way of features and I've given up on getting the mass of jar files to work out. How can I get this to work with the Android HTTP client and some requests?

I'm able to use the AccountManager to get an account and token, but unable to do something as simple as listing files in Drive because the api url takes and api key, but not a user token.

How can I accomplish the task of 1. creating a spreadsheet and 2. reading its contents back with plain old http requests?

Upvotes: 1

Views: 1654

Answers (1)

JunYoung Gwak
JunYoung Gwak

Reputation: 2987

You can achieve your goal by simple HTTP GET and POST request.

Here is a simple tutorial of how to make HTTP POST request on Android and you probably can easily search better examples simply by searching them more.

Here is an example of how to create an empty spreadsheet file

POST https://www.googleapis.com/drive/v2/files?key={YOUR_API_KEY}

Request Body:     
{
 "mimeType": "application/vnd.google-apps.spreadsheet",
 "title": "My new spreadsheet",
 "parents": [
  {
   "id": "{ROOT_FOLDER_ID}"
  }
 ]
}

For your reference, you would like to look for more options from Files.insert()

Also, in order to get contents of the spreadsheet, you would like to use Files.list(). Here is an example of how to use Files.list()

# Get list of files that are Google Drive spreadsheets
GET https://www.googleapis.com/drive/v2/files?q=mimeType%3D+%22application%2Fvnd.google-apps.spreadsheet%22&key={YOUR_API_KEY}

By using this command, you will get list of file resource. And among long contents of file resource, you want exportLinks attribute:

"exportLinks": {
  "application/pdf": "https://docs.google.com/feeds/download/spreadsheets/Export?key={FILE_KEY}&exportFormat=pdf",
  "application/x-vnd.oasis.opendocument.spreadsheet": "https://docs.google.com/feeds/download/spreadsheets/Export?key={FILE_KEY}&exportFormat=ods",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "https://docs.google.com/feeds/download/spreadsheets/Export?key={FILE_KEY}&exportFormat=xlsx"
 },

With this link, all you have to do is to make HTTP GET request and get the contents of this file from its body.

I recommend you looking through Google Drive API in general.

Also, if you want some complicated works using Drive Spreadsheet, I would recommend you having a look at Google Apps Script.

Have fun!

Upvotes: 1

Related Questions