問題描述
我整天都在與這段代碼作斗爭,但沒有運氣.我從以下代碼開始 示例來自 Google.
I have been battling this code the whole day without luck. I started by following this code sample from Google.
問題是該文件夾已成功創(chuàng)建,但在 onResult() 內,我總是得到一個無效或不完整的 DriveId 或 resourceId.這意味著我無法在我創(chuàng)建的文件夾中創(chuàng)建文件.這是我正在使用的代碼:
The problem is that the folder gets created successfully but inside onResult(), I always get a DriveId or resourceId that is invalid or incomplete. That means I cannot create a file inside the folder I created. Here is the code I am using:
public class CreateFolderActivity extends BaseDemoActivity {
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("MyAppFolder").build();
Drive.DriveApi.getRootFolder(getGoogleApiClient()).createFolder(
getGoogleApiClient(), changeSet).setResultCallback(callback);
}
final ResultCallback<DriveFolderResult> callback = new ResultCallback<DriveFolderResult>() {
@Override
public void onResult(DriveFolderResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create the folder");
return;
}
// this value is always invalid with ending == operators
Log.d("DRIVEID", "Created a folder: " + result.getDriveFolder().getDriveId());
}
};
}
每當我運行此代碼時,我都會得到以下看起來不完整的 id:
Whenever I run this code, I get the following id which appears incomplete:
CAESABi2VyCCzdWOuVMoAQ==
我不知道這里發(fā)生了什么!
I don't know what is happening here!
我在 Google 上搜索并閱讀了添加偵聽器以偵聽完成事件,但它們似乎都不起作用.
I have Google'd around and read of adding listeners to listen for completion events but none of them seem to work.
我在 SO 上看到了幾乎類似的問題,但沒有一個對我有用.
I have seen nearly similar questions on SO on this but none of them work for me.
我在應用創(chuàng)建后通過瀏覽器手動復制了 FolderId,然后粘貼到我的 android 代碼中,應用成功創(chuàng)建了一個文件.但這不是事情應該如何運作的方式.
I manually copied the FolderId through my browser after the app created it and then pasted to my android code and the app created a file successfully. But this is not how things should work.
我想等待同步完成嗎?如果是,如何?
Am I suppose to wait for the sync to complete and if so, how?
提前謝謝你!
推薦答案
你的問題大概可以找到答案 這里.你得到的 DriveId 沒問題,但你不應該直接處理它.它是一個'初步' DriveId,在提交對象后會發(fā)生變化(再次,請參閱 SO 22874657).您可以比較您獲得的 DriveId 與您將在onCompletion(CompletionEvent 事件)"中獲得的 DriveId 來測試它.
The answer to your problem can probably be found here. The DriveId you're getting is OK, but you should not handle it directly. It is a 'preliminary' DriveId that changes after the object has been committed (again, see SO 22874657). You can test it comparing DriveId you're getting vs. DriveId you'll get in 'onCompletion(CompletionEvent event)'.
這只是 GDAA 邏輯的副作用之一,可以保護您免受在線/離線網(wǎng)絡狀態(tài)的影響,從而導致無法預測的延遲.你只需要依賴回調.
This is just one of the side effects of GDAA's logic, shielding you from on-line / off-line network state resulting in unpredictable delays. You just have to rely on callbacks.
但令我驚訝的是,您不能立即將此初步"DriveId(如果是文件夾)用作另一個對象(文件夾/文件)的父級.我從未經(jīng)歷過,立即將初步"DriveId 傳遞給另一個 GDAA 方法.
在 ResourceId 的情況下是不同的.那個在 GDAA 中是次要的,僅在您離開設備時使用.在提交(上傳)對象之前,GDAA 并不知道.
But I am surprised that you can't use this 'preliminary' DriveId (in case of a folder) immediately as a parent of another object (folder/file). I have never experienced it, passing the 'preliminary' DriveId immediately to another GDAA method.
It is different in case of the ResourceId. That one is secondary in the GDAA and is used only if you go outside of the device. It is not known to the GDAA until the object is committed (uploaded).
我在這個 demo 中使用了類似的邏輯(創(chuàng)建文件夾/文件樹)(參見 MainActivity.createTree() 方法).歡迎您挖掘它.
I used similar logic (creating folder / file tree) in this demo (see MainActivity.createTree() method). You're welcome to dig in it.
中討論了一個相關問題SO 34318220.
祝你好運
這篇關于Google Drive Android API - 無效的 DriveId 和 Null ResourceId的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!