2017-12-22 13 views
0

Googleドライブの既存のファイルからデータを取得しようとしています。検索によって正しくメタデータが返されますが、ファイルの最終変更日を取得しようとするとnullになります。google.api.clientがファイルからヌルを取得する

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()'を呼び出します

答えて

0

あなたが最初にGoogleのクライアントに接続する必要があります。ここに する例です。

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

一度クライアントが接続されると、ファイルからデータを取得することができます

+0

接続されていない場合、結果は得られません。私はこのコードを使用します:[https://developers.google.com/drive/v3/web/quickstart/android] – user2847219

+0

はい。最初に接続する必要があります。 –

+0

サンプルコードを見てください。https://github.com/googledrive/android-quickstart –

関連する問題