【问题标题】:google.api.client get null from filegoogle.api.client 从文件中获取空值
【发布时间】:2017-12-22 06:53:07
【问题描述】:

我正在尝试从谷歌驱动器中的现有文件中获取一些数据。搜索正确地返回了一些元数据,但是当我尝试获取文件的最后修改日期时,我得到空值。没看懂
这是我的代码的一部分:

FileList result = null;
        try {
            result = mService.files().list()
                    .setQ("name = file.db and trashed = false")
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
 Log.d("Sync_drive", result);
 List<com.google.api.services.drive.model.File> files;
  if (result != null) {
  for (com.google.api.services.drive.model.File fileD : files) {
 dateModified = String.valueOf(fileD.getModifiedTime().getValue());
 gdid = fileD.getId();
 Log.d("Sync_drive", "id: " + gdid);
 }

结果:

Sync_drive: {"files":[{"id":"1jX2w7F0Pjx28ug0lvjEIp4Kje6fw5JyF","kind":"drive#file","mimeType":"application/octet-stream","name":"file.db"}],"incompleteSearch":false,"kind":"drive#fileList"}

进程:PID:27987 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“long com.google.api.client.util.DateTime.getValue()”

【问题讨论】:

    标签: java android rest api drive


    【解决方案1】:

    您首先需要连接到谷歌客户端: 这是一个例子:

    protected synchronized void buildGoogleApiClient() {
            if (checkPlayServices()) {
                        .requestServerAuthCode(BuildConfig.GOOGLE_WEB_CLIENT_ID)
                        .requestEmail()
                        .build();
                // Build a GoogleApiClient with access to the Google Sign-In API and the
                // options specified by gso.
                mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .addApi(/*Api for google Drive*/)
                        .enableAutoManage(this, this)
                        .build();
                mGoogleApiClient.connect();
    
            }
        }
    

    您还必须在同一个活动中实现回调函数: , GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks

    一旦客户端连接,您可以从文件中获取数据

    【讨论】:

    • 如果没有连接,我不会得到任何结果,对吧?我使用此代码:[developers.google.com/drive/v3/web/quickstart/android]
    • 我用这种方法连接它:while (!isGooglePlayServicesAvailable()) { acquireGooglePlayServices(); }如果它没有连接我不会有任何结果,但是,正如你从我的帖子中看到的,我得到了所有的元数据,除了日期
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2013-09-25
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多