[英]Google Drive for Android SDK Doesn't List Files
Google Drive Android SDK確實存在一個奇怪的問題。 我已經使用它幾個月了,直到上周它的表現都非常出色。 但是,現在確實有一個奇怪的錯誤,這種錯誤不會一直發生,而是十分之九。
我正在嘗試列出存儲在特定Google雲端硬盤文件夾中的用戶文件和文件夾。 當我嘗試使用Drive.files()。list()。execute()方法時,實際上10次中有9次沒有任何反應。 該方法只是掛起,即使我將其放置一個小時,它仍然會繼續執行...無任何操作。
我正在使用的代碼如下-所有這些都在AsyncTask的doInBackground中運行。 我已經檢查過憑據-都很好,就像應用程序的證書的SHA1哈希一樣。 沒有異常被拋出。 Google搜索沒有任何結果。 這是令我困擾的特定代碼:
try {
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
SettingsActivity.this, Arrays.asList(DriveScopes.DRIVE));
if (googleAccountName != null && googleAccountName.length() > 0) {
credential.setSelectedAccountName(googleAccountName);
Drive service = new Drive.Builder(AndroidHttp.newCompatibleTransport(),
new GsonFactory(), credential).build();
service.files().list().execute(); // Google Drive fails here
} else {
// ...
}
} catch (final UserRecoverableAuthIOException e) {
// Authorisation Needed
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
startActivityForResult(e.getIntent(), REQUEST_AUTHORISE_GDRIVE);
} catch (Exception e) {
Log.e("SettingsActivity: Google Drive", "Unable to add Google Drive account due to Exception after trying to show the Google Drive authroise request intent, as the UserRecoverableIOException was originally thrown. Error message:\n" + e.getMessage());
}
}
});
Log.d("SettingsActivity: Google Drive", "UserRecoverableAuthIOException when trying to add Google Drive account. This is normal if this is the first time the user has tried to use Google Drive. Error message:\n" + e.getMessage());
return;
} catch (Exception e) {
Log.e("SettingsActivity: Google Drive", "Unable to add Google Drive account. Error message:\n" + e.getMessage());
return;
}
我正在使用Drive API v2。 感謝大家!
玩了更多之后,事實證明這不僅僅是列出文件。 嘗試與Google雲端硬盤上的任何文件進行交互的行為方式相同-刪除,下載,創建...任何操作! 我還注意到,將設備置於飛行模式以使其無法訪問互聯網也沒有任何區別:Google雲端硬盤不會引發異常,甚至不會返回異常,它只會凍結其所在的線程。
我已經更新到最新的Drive API庫,但沒有幫助。 我記得該錯誤是在我將JSch SSH庫添加到項目后不久發生的,因此我將其刪除了,但是沒有什么區別。 刪除並重新添加Drive API v2並沒有影響,也沒有清理項目。
我發現了一些可能很重要的東西。 在Google Developer控制台上,我記錄了一些雲端硬盤錯誤,如下所示:
TOP ERRORS:
Requests % Requests Methods Error codes
18 38.30% drive.files.list 400
14 29.79% drive.files.insert 500
11 23.40% drive.files.update 500
4 8.51% drive.files.get 400
您認為這些是錯誤嗎? 我該如何解決? 謝謝
這是我的代碼,可以正常工作
new AsyncTask<Void, Void, List<File>>() {
@Override
protected List<File> doInBackground(Void... params) {
List<File> result = new ArrayList<File>();
try {
com.google.api.services.drive.Drive.Files.List list = service.files().list();
list.setQ("'" + sourcePath + "' in parents");
FileList fileList = list.execute();
result = fileList.getItems();
if(result != null) {
return result;
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(List<File> result) {
//This is List file from Google Drive
};
}.execute();
我想出了一個行之有效的解決方案,並認為我會將其發布,以便其他人在碰巧遇到問題時可以看到它。
幸運的是,我已經備份了該應用程序的所有先前版本。 因此,我將整個項目恢復到兩周前的狀態,復制並粘貼了自那時以來所做的較新版本中的所有更改,並且它可以正常工作。 我不知道為什么這應該起作用,因為最終結果是同一個項目,但是可以!
Google雲端硬盤列表文件
這可能會為您提供幫助。.嘗試在ListView中顯示它,您將看到所有提取的文件夾
public void if_db_updated(Drive service)
{
try {
Files.List request = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'");
FileList files = request.execute();
for(File file : files.getItems())
{
String title = file.getTitle();
showToast(title);
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
public void showToast(final String toast) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.