用于Android的Google云端硬盘sdk,在将文件上传到Google云端硬盘时获取错误400错误请求

时间:2013-07-03 18:19:42

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

我创建了一个试用Android SDK的应用。它之前对我有用(获取身份验证令牌,拍照并上传到Google云端硬盘文件夹)。最近,我的应用无法将内容上传到Google云端硬盘。下面是执行上传的代码段,它基本上检查文件是否存在,并决定使用insert()或update()。它之前适用于我,但现在我尝试将文件上传到Google云端硬盘。在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已经改变,但是,当我执行上传时,我仍然得到相同的错误消息,告诉我不支持媒体类型''。 (或者它认为它是空的)。谁知道会发生什么?我搜索过以前的问题,但他们确实提供了帮助

  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;

}

谁知道如何解决这个问题?谢谢〜

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

确保包含

 <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

上的后续视频

希望这有帮助。

答案 2 :(得分:0)

第一行:

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

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