4

我的 Google Drive 中有一个电子表格文件,我想从 Android 应用程序同步它。成功同步后,我的应用中有一些搜索条件,我希望从我同步的文件中获得基于此条件的结果。我是第一次这样做,并没有得到好的资源。

1) 如何在 Android App 中同步 Google Drive 文件?

2) 如何从我已同步的电子表格中检索特定数据?

注意- 我也对Google Docs API 和 Google Drive SDK感到困惑。

在我的应用程序中使用什么?

我每次只有一个文件要同步,而不是所有文件。并且该文件使用私钥对所有人开放。

我已经实现了一些代码,但它不工作。下面是我想在我的 android 应用程序中同步的电子表格公共 url。

我正在按照这里的步骤进行操作。

我的测试电子表格 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();
}
4

1 回答 1

3

我得到了它。我没有添加权限,这就是它给我错误的原因。我们需要添加以下权限进行授权。

<uses-permission android:name="android.permission.ACCOUNT_MANAGER" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
于 2012-10-09T09:02:53.923 回答