【问题标题】:Google Drive sdk for android, Getting error 400 Bad Request when uploading file to Google Drive适用于 android 的 Google Drive sdk,将文件上传到 Google Drive 时出现错误 400 Bad Request
【发布时间】:2013-07-03 18:19:42
【问题描述】:

我创建了一个应用程序来试用 Android SDK。它以前对我有用(获取身份验证令牌,拍照并上传到 Google Drive 文件夹)。最近,我的应用无法将内容上传到 Google 云端硬盘。下面是上传的一段代码,它主要检查文件是否存在并决定使用 insert() 或 update()。它以前对我有用,但现在当我尝试将文件上传到 Google Drive 时。在我在 GoogleDrive 中成功创建文件夹后,下一步就是上传文件,但每次我都会收到错误消息:

07-03 13:49:54.824: W/System.err(31132): com.google.api.client.googleapis.json.
GoogleJsonResponseException: 400 Bad Request
07-03 13:49:54.824: W/System.err(31132): {
07-03 13:49:54.824: W/System.err(31132):   "code": 400,
07-03 13:49:54.824: W/System.err(31132):   "errors": [
07-03 13:49:54.824: W/System.err(31132):     {
07-03 13:49:54.824: W/System.err(31132):       "domain": "global",
07-03 13:49:54.824: W/System.err(31132):       "message": "Media type '' is not supported. Valid media types: [*/*]",
07-03 13:49:54.824: W/System.err(31132):       "reason": "badContent"
07-03 13:49:54.824: W/System.err(31132):     }
07-03 13:49:54.824: W/System.err(31132):   ],
07-03 13:49:54.824: W/System.err(31132):   "message": "Media type '' is not supported. Valid media types: [*/*]"

我最初的猜测是我忘记在代码中为上传正文设置 MIMETYPE,是我更新了我的代码并在执行之前设置了 mimetype。在日志中,我可以看到 mimetype 已更改,但是,当我执行上传时,我仍然收到相同的错误消息,告诉我不支持 Media type ''。 (或者它认为它是空的)。有谁知道可能发生了什么?我搜索了以前的问题,但它们确实有帮助。

  private com.google.api.services.drive.model.File processGDFile(Drive service,
  String parentId, File localFile) throws Exception {
Log.i(TAG, "We are in processGDFile");
boolean existed = false;
com.google.api.services.drive.model.File processedFile;
// Determine whether the file exists in this folder or not.
String q = "'" + parentId + "' in parents and" + "title = " + "'"
    + localFile.getName() + "'";

FileContent mediaContent = new FileContent("", localFile);

FileList resultFileList = ExcuteQuery(q);

try {
  // if this file already exists in GD, we do update
  if (!resultFileList.getItems().isEmpty()) { 
    Log.i(TAG, "the file exists, use update....");
    existed = true;
    com.google.api.services.drive.model.File gdFile = resultFileList
        .getItems().get(0);
    Log.i(TAG, "MIMETYPE:" + gdFile.getMimeType());
    gdFile.setMimeType("image/jpeg");
    Log.i(TAG, "MIMETYPE_after:" + gdFile.getMimeType());

    processedFile = service.files()
        .update(gdFile.getId(), gdFile, mediaContent).execute();


    // Uncomment the following line to print the File ID.
    Log.i(TAG, "Processed File ID: %s" + processedFile.getId());

  } else {
    Log.i(TAG, "the file is new, create its meta data");
    // Start the new File's metadata.
    com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
    body.setTitle(localFile.getName());


    // Set the parent folder.
    if (parentId != null && parentId.length() > 0) {
      body.setParents(Arrays.asList(new ParentReference().setId(parentId)));
    }
    Log.i(TAG, " before insert body");

    Log.i(TAG, "MIMETYPE:" + body.getMimeType());
    body.setMimeType("image/jpeg");
    Log.i(TAG, "MIMETYPE_after:" + body.getMimeType());
    processedFile = service.files().insert(body, mediaContent).execute();

    Log.i(TAG, "Processed File ID: %s" + processedFile.getId());

  }
} catch (Exception e) {
  throw e;
}
return processedFile;

}

有人知道如何解决这个问题吗?谢谢~

【问题讨论】:

    标签: android google-drive-api google-play


    【解决方案1】:

    您似乎在 File 对象中设置 mime 类型,但您需要在 FileContent 对象中设置它。

    创建文件内容时,可以尝试传入类型 构造函数 new FileContent("image/jpeg", localFile); 或使用方法 setType("image/jpeg"); 但在您的 mediaContent 实例中,而不是在您的 body 实例中。

    【讨论】:

      【解决方案2】:

      确保包含

       <data android:mimeType="application/vnd.google-apps.drive-sdk.123456yourclientid" />
      

      在您的 xml 中。其中 123456yourclientid 是您来自 Google API 控制台的客户端 ID。

      这个页面上的视频很有帮助Quickstart: Run a Drive App on Android

      但也请务必观看Integrate with the Android Drive App 上的后续视频

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        第一行:

        FileContent mediaContent = new FileContent("", localFile);
        

        它包含一个空字符串,而不是格式 'type/subtype' 中的 mime 类型为 'image/jpeg'。 这应该在使用插入执行请求之前完成。请这样做,您将不会看到 400 错误代码(错误请求)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-02-17
          • 2016-10-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多