【问题标题】:Google Drive for Android SDK Doesn't List Files适用于 Android SDK 的 Google Drive 不列出文件
【发布时间】:2013-12-28 21:41:05
【问题描述】:

我在使用 Google Drive Android SDK 时遇到了一个非常奇怪的问题。我已经使用它几个月了,直到上周它表现完美。但是,现在出现了一个非常奇怪的错误,它并非一直发生,而是 10 次中有 9 次发生。

我正在尝试列出存储在特定 Google Drive 文件夹中的用户文件和文件夹。当我尝试使用 Drive.files().list().execute() 方法时,10 次中有 9 次实际上没有任何反应。该方法只是挂起,即使我离开它一个小时,它仍然在做......什么都没有。

我正在使用的代码如下 - 所有这些都在 AsyncTask 的 doInBackground 中运行。我检查了凭据 - 它们都很好,应用程序证书的 SHA1 哈希也是如此。不抛出异常。谷歌搜索一无所获。这是困扰我的特定代码:

    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 Drive 上的任何文件进行交互的行为方式相同 - 删除、下载、创建......任何东西!我还注意到,将设备置​​于飞行模式以使其无法访问互联网也没有什么区别:Google Drive 不会抛出异常,甚至不会返回,它只会冻结它所在的线程。

我已更新到最新的 Drive API 库,但这并没有帮助。我记得在我将 JSch SSH 库添加到项目后不久就发生了错误,所以我删除了它,但没有任何区别。删除和重新添加 Drive API v2 也没有任何区别,清理项目也没有。

编辑 2

我发现了一些可能很重要的东西。在 Google Developer 控制台上,我记录了一些 Drive 错误,如下所示:

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

您认为这些是错误吗?我该如何修复它们?谢谢

【问题讨论】:

    标签: java android google-drive-api


    【解决方案1】:

    这是我的代码,它可以工作

    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 API 控制台中检查您的 SHA1,如果您更改密钥库会失败
    • 我刚刚在 Google Cloud Console 上进行了检查。我在那里有调试和发布密钥库 SHA1。他俩都对!我怀疑它更多是在客户端,因为正如我在原始问题中指出的那样,即使设备处于飞行模式,Drive API 的行为也很奇怪。
    【解决方案2】:

    我想出了一个可行的解决方案,并认为我会发布它,以便其他人在遇到问题时可以看到它。

    幸运的是,我已经备份了该应用程序的所有早期版本。因此,我将整个项目恢复到两周前的状态,复制并粘贴了自那时以来所做的较新版本的所有更改,并且成功了。我不明白为什么这应该起作用,因为最终结果是同一个项目,但确实如此!

    【讨论】:

      【解决方案3】:

      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();
                }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多