google.api.client从文件中获取null

问题描述 投票:0回答:1

我正试图从谷歌硬盘中的现有文件中获取一些数据。搜索正确地返回了一些元数据,但是当我尝试获取文件的最后修改日期时,我得到null。我不明白 这是我的代码的一部分:

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个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.