Java Android SDK的谷歌驱动不';t列出文件

Java Android SDK的谷歌驱动不';t列出文件,java,android,google-drive-api,Java,Android,Google Drive Api,我对Google Drive Android SDK有一个非常奇怪的问题。我已经用了好几个月了,直到上周它还表现得很好。然而,现在有一个非常奇怪的错误,它不是一直都发生,但10次中有9次发生 我试图列出用户的文件和文件夹存储在一个特定的谷歌驱动器文件夹。当我尝试使用Drive.files().list().execute()方法时,10次中有9次实际上什么都没有发生。该方法只是挂起,即使我离开它一个小时,它仍然在做。。。没什么 下面是我使用的代码——所有这些都是在异步任务的doInBackgro

我对Google Drive Android SDK有一个非常奇怪的问题。我已经用了好几个月了,直到上周它还表现得很好。然而,现在有一个非常奇怪的错误,它不是一直都发生,但10次中有9次发生

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

下面是我使用的代码——所有这些都是在异步任务的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;
    }
我使用的是驱动器API v2。谢谢大家

编辑 玩了一点之后,结果证明这不仅仅是为了列出文件。尝试与Google Drive上的任何文件进行交互的行为都是相同的-删除、下载、创建。。。任何东西我还注意到,将设备置于飞机模式,使其无法访问互联网也没有什么区别:Google Drive不会抛出异常,甚至不会返回,它只是冻结了它所在的线程

我已经更新到最新的驱动器API库,但这没有帮助。我记得这个错误是在我将JSCHSSH库添加到项目中后不久发生的,所以我删除了它,但是没有什么不同。删除和重新添加驱动器API v2也没有任何区别,清理项目也没有任何区别

编辑2 我发现了一些有意义的东西。在Google开发者控制台上,我记录了一些驱动器错误,如下所示:

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();
newasynctask(){
@凌驾
受保护列表doInBackground(无效…参数){
列表结果=新建ArrayList();
试试{
com.google.api.services.drive.drive.Files.List=service.Files().List();
list.setQ(“父级中的“”+sourcePath+”);
FileList FileList=list.execute();
结果=fileList.getItems();
如果(结果!=null){
返回结果;
}
}捕获(UserRecoverableAuthIOE异常){
startActivityForResult(如getIntent(),请求授权);
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
} 
受保护的void onPostExecute(列表结果){
//这是谷歌硬盘上的列表文件
};
}.execute();

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


幸运的是,我已经备份了该应用程序的所有早期版本。因此,我将整个项目恢复到两周前的状态,复制并粘贴了从那时起对新版本所做的所有更改,并且工作正常。我不明白为什么这应该有效,因为最终结果是相同的项目,但它确实有效

谷歌驱动器列表文件

这可能会对您有所帮助。。尝试在ListView中显示它,您将看到所有获取的文件夹


我只是试了一下代码,但是很抱歉,什么也没发生。它像以前一样挂起了。你想让我发布服务的代码吗?正如我所说的,直到上周,一切——包括服务——都运转良好,所以我无法想象会是这样。但是如果你想看一看,我可以发布它,我现在已经为你发布了更完整的代码。有什么看起来不对劲的地方吗?也许你需要在谷歌API控制台中检查你的SHA1,如果你更改了你的密钥库,那将会失败。我刚刚在谷歌云控制台上检查过。我有调试和发布密钥库SHA1。他们都是对的!我怀疑更多的是在客户端,因为正如我在最初的问题中所指出的,即使设备处于飞机模式,驱动API的行为也很奇怪。
 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();
          }
        });