Search code examples
javaandroidrestapidrive

google.api.client get null from file


I'm trying to get some data from an existing file in google drive. The search correctly returns me some metadata, but when I try to get the date of the last modification of the file, I get null. I do not understand
This is a part of my code:

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);
 }

result:

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

and

Process: PID: 27987 java.lang.NullPointerException: Attempt to invoke virtual method 'long com.google.api.client.util.DateTime.getValue()' on a null object reference


Solution

  • you first need to connect to google client: here is an example :

    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();
    
            }
        }
    

    you also have to implement callback functions in the same activity: , GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks

    once client is connected ,you can get data from file