简体   繁体   English

使用Android版Google Drive API将JSON文件上传到Google Drive

[英]Upload JSON file to Google Drive using Google Drive API for android programatically

I know from the sample code how to upload a text file into the Drive AppFolder, but from the docs is not clear how to extend it to JSON, if it's possible at all. 我从示例代码中知道如何将文本文件上传到Drive AppFolder,但是从文档中还不清楚如何将其扩展为JSON(如果可能的话)。

Here is the code for uploading a Text File: 这是上传文本文件的代码:

final private ResultCallback<DriveContentsResult> driveContentsCallback = new
        ResultCallback<DriveContentsResult>() {
    @Override
    public void onResult(DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create new file contents");
            return;
        }
        final DriveContents driveContents = result.getDriveContents();

        // Perform I/O off the UI thread.
        new Thread() {
            @Override
            public void run() {
                // write content to DriveContents
                OutputStream outputStream = driveContents.getOutputStream();
                Writer writer = new OutputStreamWriter(outputStream);
                try {
                    writer.write("Hello World!");
                    writer.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage());
                }

                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                        .setTitle("New file")
                        .setMimeType("text/plain")
                        .setStarred(true).build();

                // create a file on root folder
                Drive.DriveApi.getRootFolder(getGoogleApiClient())
                        .createFile(getGoogleApiClient(), changeSet, driveContents)
                        .setResultCallback(fileCallback);
            }
        }.start();
    }
};

I need to change the .setMimeType() and the outputStream , but to what exactly? 我需要更改.setMimeType()outputStream ,但是到底是什么?

my JSON String is declared as follows: 我的JSON字符串声明如下:

String json = gson.toJson(activeSubs); //activeSubs being an ArrayList

Any help is appreciated. 任何帮助表示赞赏。

This fixed it for me: 这为我解决了:

String json = gson.tojson(activeSubs);

final private ResultCallback<DriveContentsResult> driveContentsCallback = new
        ResultCallback<DriveContentsResult>() {
    @Override
    public void onResult(DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create new file contents");
            return;
        }
        final DriveContents driveContents = result.getDriveContents();

        // Perform I/O off the UI thread.
        new Thread() {
            @Override
            public void run() {
                // write content to DriveContents
                OutputStream outputStream = driveContents.getOutputStream();
                Writer writer = new OutputStreamWriter(outputStream);
                try {
                    writer.write(json);
                    writer.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage());
                }

                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                        .setTitle("New file.txt")
                        .setMimeType("json")
                        .setStarred(true).build();

                // create a file on root folder
                Drive.DriveApi.getRootFolder(getGoogleApiClient())
                        .createFile(getGoogleApiClient(), changeSet, driveContents)
                        .setResultCallback(fileCallback);
            }
        }.start();
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中以编程方式使用Google Drive API将图像上传到Google Drive - Upload image to a google drive using google drive api programatically in android 在 Android Studio 中使用 Google Drive API 从驱动器下载文件以上传到另一个驱动器 - Download file from drive to upload into another drive using Google Drive API in Android Studio 如何使用 Google Drive API 从 android 将加密的图像文件上传到 Google Drive - How to upload encrypted image file on google drive from android using Google Drive API 使用Google Drive Api上传文件 - Upload file using Google Drive Api 如何使用Google Drive Android API删除Google Drive上的文件 - How to delete a file on google drive using Google Drive Android API 使用Android将文本文件上传到Google云端硬盘 - Upload text file to Google Drive using Android 无法使用教程将文件上传到Google云端硬盘(Android上为Drive App) - Unable To Upload File to Google Drive Using Tutorial (Drive App on Android) 使用 Google Drive API 以编程方式登录到 Google Drive - Login to google drive programatically, using Google Drive API 谷歌驱动器配额使用android api驱动器 - google drive quota using android api for drive Google Drive Android API如何将音频文件上传到我的驱动器? 如何同步驱动器文件? - Google Drive Android API how to upload a audio file to my drive ? How to sync drive files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM