通过Android / Java中的URL将Google表格导出为CSV / JSON

时间:2018-10-09 00:15:21

标签: java android google-sheets google-drive-api google-sheets-api

我的Android应用连接到Google帐户,并且应该以CSV或JSON的形式从Google云端硬盘检索Google表格文件。

我正在使用以下URL结构:

sheetURL = "https://www.googleapis.com/drive/v3/files/"
    + sheetID + "fields=contentHints%2Fthumbnail%2FmimeType%2CcopyRequiresWriterPermission"
    + "%2CcreatedTime%2CfileExtension%2CfullFileExtension%2Cid%2Ckind%2ClastModifyingUser%2CmimeType%2CmodifiedByMeTime%2CmodifiedTime%2Cname%2CownedByMe&key=" + APP_API_KEY;

,getter代码如下:

private String downloadUrl(String urlString) throws IOException {
    InputStream is = null;
    try {
        URL url = new URL(urlString);

        Log.d("DOWNLOADURL", "URL TO CONNECT: " + url);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        int responseCode = conn.getResponseCode();
        is = conn.getInputStream();

        String contentAsString = convertStreamToString(is);
        //Log.d("DOWNLOAD_URL", "CONTENT_AS_STRING: " + contentAsString);

        return contentAsString;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

我一直在寻找这个答案很多小时。除了文件本身,我可以获取URL指定的所有元数据。可能缺少明显的东西。非常感谢您的帮助。

0 个答案:

没有答案