Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing file content in Google Drive

This is a very strange phenomenon. My app uses the Android Google Drive SDK to store two data files for every user. These files are in the app folder. To get a connection to the Google service, I use :

PlusOptions plusOptions = new Plus.PlusOptions.Builder()
            .addActivityTypes("http://schemas.google.com/AddActivity").build();
mGoogleApi = new GoogleApiClient.Builder(this).addApi(Plus.API, plusOptions).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addScope(Plus.SCOPE_PLUS_PROFILE).addApi(Drive.API).addScope(Drive.SCOPE_FILE)
            .addScope(Drive.SCOPE_APPFOLDER).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
            .build();

I am able to create these files and write them. But from time to time, the two files disappear, and their content is lost. The logcat stream contains messages like these:

05-16 10:15:18.316: W/GLSUser(829): [apk][apk] Permission for debut.kholle to access oauth2:https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me will be managed remotely.
05-16 10:15:18.416: I/qtaguid(14877): Failed write_ctrl(u 56) res=-1 errno=22
05-16 10:15:18.416: I/qtaguid(14877): Untagging socket 56 failed errno=-22
05-16 10:15:18.416: W/NetworkManagementSocketTagger(14877): untagSocket(56) failed with errno -22
05-16 10:15:18.416: I/imp(14877): I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
05-16 10:15:18.416: I/imp(14877): Retrying request
05-16 10:15:18.436: W/GLSUser(829): [apk][apk] Permission for debut.kholle to access oauth2:https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me will be managed remotely.
05-16 10:15:18.516: W/GLSUser(829): GoogleAccountDataService.getToken()
05-16 10:15:18.546: I/qtaguid(21108): Failed write_ctrl(u 85) res=-1 errno=22
05-16 10:15:18.546: I/qtaguid(21108): Untagging socket 85 failed errno=-22
05-16 10:15:18.546: W/NetworkManagementSocketTagger(21108): untagSocket(85) failed with errno -22
05-16 10:15:18.546: I/imp(21108): I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
05-16 10:15:18.546: I/imp(21108): Retrying request
05-16 10:15:19.576: E/SyncSchedulerImpl(14877): Ignoring sync request:  rate limited (on connection limit)

Edit:

Here is my code to read a file containing application data. This code is included in the activity onCreate procedure:

monGoogleApiLire = new GoogleApiClient.Builder(this).addApi(Drive.API).addScope(Drive.SCOPE_FILE)
            .addScope(Drive.SCOPE_APPFOLDER).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {

                @Override
                public void onConnected(Bundle connectionHint) {
                    Filter titreFilter = Filters.eq(SearchableField.TITLE, NOM_FICHIER_PLANNING);
                    Query query = new Query.Builder().addFilter(titreFilter).build();
                    Drive.DriveApi.getAppFolder(monGoogleApiLire).queryChildren(monGoogleApiLire, query)
                            .setResultCallback(new ResultCallback<MetadataBufferResult>() {

                                @Override
                                public void onResult(MetadataBufferResult result) {
                                    if (!result.getStatus().isSuccess()) {
                                        // showMessage("Error while trying to create the file");
                                        return;
                                    }
                                    MetadataBuffer meta = result.getMetadataBuffer();
                                    if (meta.getCount() != 0) {
                                        int nb = meta.getCount();
                                        DriveId idc = null;
                                        for (int i = 0; i < nb; i++) {
                                            idc = meta.get(i).getDriveId();
                                        }
                                        DriveFile fichier = Drive.DriveApi.getFile(monGoogleApiLire, idc);
                                        fichier.openContents(monGoogleApiLire, DriveFile.MODE_READ_ONLY, null)
                                                .setResultCallback(new ResultCallback<ContentsResult>() {
                                                    @Override
                                                    public void onResult(ContentsResult result) {
                                                        if (!result.getStatus().isSuccess()) {
                                                            // display an error saying file can't be opened
                                                            return;
                                                        }
                                                        // Contents object contains pointers
                                                        // to the actual byte stream
                                                        Contents contents = result.getContents();
                                                        BufferedReader reader = new BufferedReader(
                                                                new InputStreamReader(contents.getInputStream()));
                                                        StringBuilder builder = new StringBuilder();
                                                        String line;
                                                        try {
                                                            while ((line = reader.readLine()) != null) {
                                                                builder.append(line);
                                                            }
                                                            String contentsAsString = builder.toString();
                                                            reader.close();

                                                            }

                                                        } catch (IOException e) {
                                                            // TODO Auto-generated catch block
                                                            e.printStackTrace();
                                                        } catch (JSONException e) {
                                                            // TODO Auto-generated catch block
                                                            e.printStackTrace();
                                                        }
                                                        contents.close();
                                                        // fichier.commitAndCloseContents(gapi, contents);
                                                    }
                                                });
                                    } else {
                                        monPlanning = new ArrayList<DonneesPlanning>();
                                        afficherPlanning();
                                        if (monPlanning != null && mesCours != null && mesLieux != null) {
                                            ActiviteKholle.this.setProgressBarIndeterminateVisibility(false);
                                            monGoogleApiLire.disconnect();
                                        }
                                        // monGoogleApiLire.disconnect();
                                    }
                                    meta.close();
                                }

                            });
                    titreFilter = Filters.eq(SearchableField.TITLE, NOM_FICHIER_COURS);
                    query = new Query.Builder().addFilter(titreFilter).build();
                    Drive.DriveApi.getAppFolder(monGoogleApiLire).queryChildren(monGoogleApiLire, query)
                            .setResultCallback(new ResultCallback<MetadataBufferResult>() {

                                @Override
                                public void onResult(MetadataBufferResult result) {
                                    if (!result.getStatus().isSuccess()) {
                                        // showMessage("Error while trying to create the file");
                                        return;
                                    }
                                    MetadataBuffer meta = result.getMetadataBuffer();
                                    if (meta.getCount() != 0) {
                                        int nb = meta.getCount();
                                        DriveId idc = null;
                                        for (int i = 0; i < nb; i++) {
                                            idc = meta.get(i).getDriveId();
                                        }
                                        DriveFile fichier = Drive.DriveApi.getFile(monGoogleApiLire, idc);
                                        fichier.openContents(monGoogleApiLire, DriveFile.MODE_READ_ONLY, null)
                                                .setResultCallback(new ResultCallback<ContentsResult>() {
                                                    @Override
                                                    public void onResult(ContentsResult result) {
                                                        if (!result.getStatus().isSuccess()) {
                                                            // display an error saying file can't be opened
                                                            return;
                                                        }
                                                        // Contents object contains pointers
                                                        // to the actual byte stream
                                                        Contents contents = result.getContents();
                                                        BufferedReader reader = new BufferedReader(
                                                                new InputStreamReader(contents.getInputStream()));
                                                        StringBuilder builder = new StringBuilder();
                                                        String line;
                                                        try {
                                                            while ((line = reader.readLine()) != null) {
                                                                builder.append(line);
                                                            }
                                                            String contentsAsString = builder.toString();
                                                            reader.close();

                                                        } catch (IOException e) {
                                                            // TODO Auto-generated catch block
                                                            e.printStackTrace();
                                                        } catch (JSONException e) {
                                                            // TODO Auto-generated catch block
                                                            e.printStackTrace();
                                                        }
                                                        contents.close();
                                                        // fichier.commitAndCloseContents(gapi, contents);
                                                    }
                                                });
                                    } else {
                                        mesCours = new ArrayList<DonneesCours>();
                                        if (monPlanning != null && mesCours != null && mesLieux != null) {
                                            ActiviteKholle.this.setProgressBarIndeterminateVisibility(false);
                                            monGoogleApiLire.disconnect();
                                        }
                                    }
                                    meta.close();
                                }

                            });
                    titreFilter = Filters.eq(SearchableField.TITLE, NOM_FICHIER_LIEUX);
                    query = new Query.Builder().addFilter(titreFilter).build();
                    Drive.DriveApi.getAppFolder(monGoogleApiLire).queryChildren(monGoogleApiLire, query)
                            .setResultCallback(new ResultCallback<MetadataBufferResult>() {

                                @Override
                                public void onResult(MetadataBufferResult result) {
                                    if (!result.getStatus().isSuccess()) {
                                        // showMessage("Error while trying to create the file");
                                        return;
                                    }
                                    MetadataBuffer meta = result.getMetadataBuffer();
                                    if (meta.getCount() != 0) {
                                        int nb = meta.getCount();
                                        DriveId idc = null;
                                        for (int i = 0; i < nb; i++) {
                                            idc = meta.get(i).getDriveId();
                                        }
                                        DriveFile fichier = Drive.DriveApi.getFile(monGoogleApiLire, idc);
                                        fichier.openContents(monGoogleApiLire, DriveFile.MODE_READ_ONLY, null)
                                                .setResultCallback(new ResultCallback<ContentsResult>() {
                                                    @Override
                                                    public void onResult(ContentsResult result) {
                                                        if (!result.getStatus().isSuccess()) {
                                                            // display an error saying file can't be opened
                                                            return;
                                                        }
                                                        // Contents object contains pointers
                                                        // to the actual byte stream
                                                        Contents contents = result.getContents();
                                                        BufferedReader reader = new BufferedReader(
                                                                new InputStreamReader(contents.getInputStream()));
                                                        StringBuilder builder = new StringBuilder();
                                                        String line;
                                                        try {
                                                            while ((line = reader.readLine()) != null) {
                                                                builder.append(line);
                                                            }
                                                            String contentsAsString = builder.toString();
                                                            reader.close();

                                                            if (monPlanning != null && mesCours != null
                                                                    && mesLieux != null) {
                                                                ActiviteKholle.this
                                                                        .setProgressBarIndeterminateVisibility(false);
                                                                monGoogleApiLire.disconnect();
                                                            }
                                                        } catch (IOException e) {
                                                            // TODO Auto-generated catch block
                                                            e.printStackTrace();
                                                        } catch (JSONException e) {
                                                            // TODO Auto-generated catch block
                                                            e.printStackTrace();
                                                        }
                                                        contents.close();
                                                        // fichier.commitAndCloseContents(gapi, contents);
                                                    }
                                                });
                                    } else {
                                        mesLieux = new ArrayList<DonneesLieu>();
                                        if (monPlanning != null && mesCours != null && mesLieux != null) {
                                            ActiviteKholle.this.setProgressBarIndeterminateVisibility(false);
                                            monGoogleApiLire.disconnect();
                                        }
                                    }
                                    meta.close();
                                }

                            });
                }

                @Override
                public void onConnectionSuspended(int cause) {
                    // TODO Auto-generated method stub

                }

            }).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {

                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    // TODO Auto-generated method stub

                }

            }).build();


monGoogleApiLire.connect();

Is there an explanation for this ?

P.S. : After a lot of trials and test, I am now sure that the field "monGoogleApi" is connected when I test the file existence. When I use "queryChildren" to search for the file by name and mime type, on the line of code :

if (meta.getCount() != 0) {
...
}

During 15 minutes, meta.getCount is equal to 1, after 15 minutes, meta.getCount equals 0.

like image 483
bobygerm Avatar asked Nov 11 '22 08:11

bobygerm


1 Answers

Have you check the connection status of "monGoogleApi" ?

This connection may have a session timeout.

https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.html#isConnected()

like image 114
Leoz Avatar answered Nov 15 '22 05:11

Leoz