我已經創建了一個應用來試用Android SDK。它以前爲我工作(獲取身份驗證令牌,拍照並上傳到Google雲端硬盤文件夾)。最近,我的應用在將內容上傳到Google雲端硬盤時出現問題。下面是上傳的代碼片段,它基本上檢查文件是否存在,並決定使用insert()或update()。它以前適用於我,但現在當我嘗試將文件上傳到Google Drive時。我已經成功地創建了Google雲端硬盤的文件夾後,再下一步就是要上傳的文件,但我每次都會得到錯誤消息時說:Google Drive sdk for android,在將文件上傳到Google Drive時出現錯誤400錯誤請求
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;
}
有誰知道如何解決這個問題?謝謝〜