android - 用于 android requestSync 的 Google Drive API 始终返回 "sync request limit exceeded"

标签 android google-drive-api google-drive-android-api

我正在使用适用于 Android 的 google drive api 来访问 google drive 中的文件。上传新文件后,我的应用程序需要 3 到 15 分钟才能访问该文件。我尝试添加 requestSync 以偶尔强制同步,但每次运行它时我都会收到“超出同步请求限制”。是否有什么原因导致我已经达到限制,或者是否存在其他问题?

代码的 RequestSync 部分:

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

全类:

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

最佳答案

操作失败,因为应用程序尝试操作的频率太高。据官方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.

关于android - 用于 android requestSync 的 Google Drive API 始终返回 "sync request limit exceeded",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36586286/

相关文章:

android - 如何使用 Google 登录保存数据

android - 通知栏广告

java - WebView canGoback() 功能上的 WebView clearHistory()?

android - Google Drive Android API ConnectionResult 错误

google-cloud-platform - 使用 GCP 后端访问 Google Colab 中的驱动器时出错

google-drive-api - 什么 "basic account information"是 Google Drive 应用程序通过身份验证范围的 "viewed"?

android - 如何在新的 Google Drive Android API 下切换帐户

android - Android Things 中使用的 .driver-metadata 文件是什么?

android - 在 Mac 应用程序中使用 adb 命令

google-apps-script - 使用 Google Drive API 时不触发 onChange 触发器