用于android requestSync的Google Drive API始终返回“超出同步请求限制”

时间:2016-04-13 00:13:10

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

我使用谷歌驱动器API为Android访问谷歌驱动器中的文件。上传新文件时,我的应用程序可以访问该文件需要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);
    }
}

}

1 个答案:

答案 0 :(得分:0)

操作失败,因为应用程序经常尝试操作。根据官方DriveApi docs

  

为避免设备和服务器过载,请同步   请求是有限的。在这种情况下,操作将失败   DRIVE_RATE_LIMIT_EXCEEDED状态,表示已同步   发生在最近,所以不需要另一个同步。该   在足够的退避后重新尝试时,操作将成功   持续时间。