3

I am using google drive api for android to access files in google drive. When a new file is uploaded it takes anywhere from 3 to 15 minutes for the file to be accessible by my app. I tried to add requestSync to occasionally force a sync but every time I run it I get "sync request limit exceeded". Is there something that can be causing me to hit the limit already, or is there a different issue?

RequestSync portion of code:

Drive.DriveApi.requestSync(getGoogleApiClient()).setResultCallback(syncCallback);

final private  ResultCallback<com.google.android.gms.common.api.Status> syncCallback = new ResultCallback<com.google.android.gms.common.api.Status>() {
    @Override
    public void onResult(Status status) {
        if (!status.getStatus().isSuccess()) {
            showMessage(status.toString());
        } else {
            showMessage("updated");
        }
    }
};

Full class:

public class SD_UAV_TEST_RESULTS extends SD_UAV_TEST_MAIN {

TextView numberOfCows_text,picsProcessed_text;
public static int previousCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sd__uav__test__results);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    numberOfCows_text = (TextView) findViewById(R.id.numberOfCows);
    picsProcessed_text = (TextView) findViewById(R.id.picsProcessed);
}


public void refreshResults(View view)
{
    getContnets();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        String temp = data.getStringExtra("parse");
        String[] lines = temp.split(":");
        int cow_count = 0;

        for(int i=0;i<lines.length;i++)
        {
            cow_count += Integer.parseInt(lines[i].split(",")[1]);
        }

        numberOfCows_text.setText(Integer.toString(cow_count));
        picsProcessed_text.setText(Integer.toString(lines.length));

    }
    if (requestCode == 8) {

    }
}

public void getContnets(){
    if(file_id != null) {
        Drive.DriveApi.fetchDriveId(getGoogleApiClient(), file_id)
                .setResultCallback(idCallback);
    }
}
public void parseFileRead(String temp)
{
    String[] lines = temp.split(":");
    int cow_count = 0;

    for(int i=0;i<lines.length;i++)
    {
        cow_count += Integer.parseInt(lines[i].split(",")[1]);
    }

    if(lines.length == previousCount)
    {
        fnfCount = fnfCount + 1;
    }
    if(fnfCount >= 5)
    {
        Drive.DriveApi.requestSync(getGoogleApiClient()).setResultCallback(syncCallback);
        fnfCount = 0;
    }
    numberOfCows_text.setText(Integer.toString(cow_count));
    picsProcessed_text.setText(Integer.toString(lines.length));
    previousCount = lines.length;
}


final private  ResultCallback<com.google.android.gms.common.api.Status> syncCallback = new ResultCallback<com.google.android.gms.common.api.Status>() {
    @Override
    public void onResult(Status status) {
        if (!status.getStatus().isSuccess()) {
            showMessage(status.toString());
        } else {
            showMessage("updated");
        }
    }
};


//----------------------------------------------------------------------------------------------

final private ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() {
    @Override
    public void onResult(DriveApi.DriveIdResult result) {
        DriveId temp = result.getDriveId();
        new RetrieveDriveFileContentsAsyncTask(
                SD_UAV_TEST_RESULTS.this).execute(temp);
    }
};

final private class RetrieveDriveFileContentsAsyncTask
        extends ApiClientAsyncTask<DriveId, Boolean, String> {

    public RetrieveDriveFileContentsAsyncTask(Context context) {
        super(context);
    }

    @Override
    protected String doInBackgroundConnected(DriveId... params) {
        String contents = null;
        DriveFile file = params[0].asDriveFile();

        DriveApi.DriveContentsResult driveContentsResult =
                file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();
        if (!driveContentsResult.getStatus().isSuccess()) {
            return null;
        }
        DriveContents driveContents = driveContentsResult.getDriveContents();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(driveContents.getInputStream()));
        StringBuilder builder = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            contents = builder.toString();
        } catch (IOException e) {
        }

        driveContents.discard(getGoogleApiClient());
        return contents;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (result == null) {
            showMessage("Error while reading from the file");
            return;
        }
        parseFileRead(result);
    }
}

}

  • Possible duplicate: http://stackoverflow.com/questions/31203255/android-google-play-drive-api / http://stackoverflow.com/questions/26420499/google-drive-api-quotas-when-access-using-google-play-services – Android Enthusiast Apr 13 '16 at 08:43
  • Are you running into "limit exceeds" every time? There's some discussion about the limits here: http://stackoverflow.com/questions/34735443/enforce-drive-api-for-android-to-work-online-mode-only – Anatoli Apr 14 '16 at 00:28
  • I am never able to get a successful sync to work no matter the time in between, it always returns the rate exceeded. – John Pollock Apr 14 '16 at 01:11

1 Answers1

0

The operation failed because the application attempted the operation too often. As per official DriveApi docs,

In order to avoid excessive load on the device and the server, sync requests are rate limited. In this case, the operation will fail with DRIVE_RATE_LIMIT_EXCEEDED status, which indicates that a sync already happened quite recently so there is no need for another sync. The operation will succeed when reattempted after a sufficient backoff duration.

Priyank Patel
  • 12,244
  • 8
  • 65
  • 85