问题描述
我正在尝试使用 Google Drive API 上传文件,但我的代码卡在了基本上试图创建文件的代码上。
我使用的是 Google Drive V3 API。 https://developers.google.com/drive/api/v3/about-auth
这是我正在尝试的代码。
GoogleAccountCredential credential =
GoogleAccountCredential.usingOAuth2(
getApplicationContext(),Collections.singleton(DriveScopes.DRIVE_FILE));
credential.setSelectedAccount(account.getAccount());
googleDriveService =
new com.google.api.services.drive.Drive.Builder(
AndroidHttp.newCompatibleTransport(),new GsonFactory(),credential)
.setApplicationName("Notes")
.build();
mDriveServiceHelper = new DriveServiceHelper(googleDriveService);
AsyncTask<Void,Void,GoogleDriveFileHolder> fileCreate = createTextFile("TestFileAJ1"," Hi. This is just a text file",null);
try {
GoogleDriveFileHolder s = fileCreate.get();
Log.i(TAG,"File Id:"+s.getId());
} catch (ExecutionException e) {
e.printstacktrace();
} catch (InterruptedException e) {
e.printstacktrace();
}
执行卡在代码中提到的那一行。
googleFile = googleDriveService.files().create(Metadata,contentStream).setFields("id")
public AsyncTask<Void,GoogleDriveFileHolder> createTextFile(final String fileName,final String content,@Nullable final String folderId) {
AsyncTask<Void,GoogleDriveFileHolder> task= new AsyncTask<Void,GoogleDriveFileHolder>() {
@Override
protected GoogleDriveFileHolder doInBackground(Void... params) {
Log.i(TAG,"Started Background activity");
List<String> root;
if (folderId == null) {
root = Collections.singletonList("root");
} else {
root = Collections.singletonList(folderId);
}
Log.i(TAG,"Into the executer.");
File Metadata = new File()
.setParents(root)
.setMimeType("text/plain")
.setName(fileName);
ByteArrayContent contentStream = ByteArrayContent.fromString("text/plain",content);
Log.i(TAG,"After content Stream");
File googleFile = null;
try {
Log.i(TAG,"Before conencting to drive");
Drive.Files.List fileList = googleDriveService.files().list();
Log.i(TAG,"File list:"+ fileList.toString());
//Code is getting stuck at this line
googleFile = googleDriveService.files().create(Metadata,contentStream).setFields("id")
.execute();
Log.i(TAG,"After conencting to drive");
} catch (IOException e) {
e.printstacktrace();
}
Log.i(TAG,"Getting Google drive file");
if (googleFile == null) {
try {
throw new IOException("Null result when requesting file creation.");
} catch (IOException e) {
e.printstacktrace();
}
}
Log.i(TAG,"Before creating file");
createAFile();
Log.i(TAG,"After creating file");
GoogleDriveFileHolder googleDriveFileHolder = new GoogleDriveFileHolder();
googleDriveFileHolder.setId(googleFile.getId());
Log.i(TAG,"File created Id:"+googleFile.getId()+" "+googleFile.getName());
return googleDriveFileHolder;
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG,"On process update :"+values.toString());
super.onProgressUpdate(values);
}
@Override
protected void onPreExecute() {
Log.i(TAG,"Execution started !!!");
super.onPreExecute();
}
};
task.execute();
return task;
}
请帮忙。
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)