简体   繁体   English

Google Drive Android API(GDAA)getResourceId()返回null(计时问题)

[英]Google Drive Android API (GDAA) getResourceId() returning null (timing issue)

When testing of delete, trash functionality discussed in SO 22295903 , I've run into this issue. 在测试SO 22295903中讨论的删除,垃圾功能时,我遇到了这个问题。

1/ Create a file with contents 1 /创建包含内容的文件

GoogleApiClient _gac;
DriveFile createFileWait(DriveFolder fldr, String name, String mime, byte[] buff) {
  DriveFile drvFile = null;
  try { 
    ContentsResult rslt = Drive.DriveApi.newContents(_gac).await();
    if (rslt.getStatus().isSuccess()) {
      Contents cont = rslt.getContents();    
      cont.getOutputStream().write(buff);
      MetadataChangeSet meta = (mime == null) ?
          new MetadataChangeSet.Builder().setTitle(name).build() :
          new MetadataChangeSet.Builder().setTitle(name).setMimeType(mime).build();
      drvFile = fldr.createFile(_gac, meta, cont).await().getDriveFile();
    }
  } catch (Exception e) {}
  return drvFile;
}

2/ retrieve the file using query (it's title): 2 /使用查询检索文件(它的标题):

ArrayList<DriveId> findAll(String title, String mime, DriveFolder fldr) {
  ArrayList<DriveId> dIDs = null;
  if (isConnected()) try {
    ArrayList<Filter> fltrs = new ArrayList<Filter>();
    fltrs.add(Filters.eq(SearchableField.TRASHED, false));
    if (title != null)  fltrs.add(Filters.eq(SearchableField.TITLE, title));
    if (mime  != null)  fltrs.add(Filters.eq(SearchableField.MIME_TYPE, mime));
    Query qry = new Query.Builder().addFilter(Filters.and(fltrs)).build(); 
    MetadataBufferResult rslt = (fldr == null) ? Drive.DriveApi.query(_gac, qry).await() : 
                                                   fldr.queryChildren(_gac, qry).await();
    if (rslt.getStatus().isSuccess()) {
      MetadataBuffer mdb = null;
      try { 
        mdb = rslt.getMetadataBuffer();
        if (mdb == null) return null;
        dIDs = new ArrayList<DriveId>();
        for (Metadata md : mdb) {
          if ((md == null) || md.isTrashed()) continue; 
          dIDs.add(md.getDriveId());
        }
      } finally { if (mdb != null) mdb.close(); } 
    }
  } catch (Exception e) {}
  return dIDs;
}

3/ You get valid DriveId. 3 /您获得有效的DriveId。 Try to use it to gain resource ID to use in RESTful API, or elsewhere. 尝试使用它来获取在RESTful API或其他地方使用的资源ID。

String fileID = drvId.getResourceId();

You get a null value. 你得到一个值。 After a few moments (random, difficult to specify), if you repeat the query, you'll finally get your resource ID. 片刻之后(随机,难以指定),如果您重复查询,您将最终获得您的资源ID。 I know why, it is probably a latency issue. 我知道为什么,这可能是一个延迟问题。 I'm just soliciting a comment from the Google Support Team . 我只是征求谷歌支持团队的评论。 Is there a way to gain control? 有办法获得控制权吗? Query latency status? 查询延迟状态?

This happens because changes are persisted locally first, and then uploaded to the server at a (possibly) later time when we have sufficient network connectivity. 发生这种情况是因为更改首先在本地持久存储,然后在我们有足够的网络连接时(可能)以后上传到服务器。 Unfortunately the resource id is not available until the newly created file is committed to the server. 遗憾的是,在将新创建的文件提交给服务器之前,资源ID不可用。

Currently all you can do is wait for it to be available. 目前你所能做的只是等待它可用。 We are working on some additions that will make this flow easier, so stay tuned. 我们正在努力增加一些能使这种流程变得更容易的内容,敬请期待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM