Android Google Play / Drive Api [英] Android Google Play / Drive Api

查看:250
本文介绍了Android Google Play / Drive Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用Google Drive Api来存储使用AppDataFolder工具的数据库。我有一个测试应用程序在一台设备上成功运行。我能够上传/更新/删除/下载数据库文件并将其重新集成到程序中,而不会出现任何问题。



我遇到的问题是当我想要与运行相同应用的其他设备共享此数据库,则事情不会按预期工作。示例设备A我上传数据库,设备B - 我想下载数据库,但没有找到文件(此延迟可能会在几秒到几小时之间变化很大)。原因 - 使用Api时,它决定何时同步数据,因为它是排队的,而不是立即上传。因此,当在一台设备上使用时,这不是问题,因为它从云存储中获取同步文件,或等待同步文件。



我尝试过各种各样的事情,例如试图列出所有AppDataFolder文件或通过查询过滤器检索元数据,然后再发出更新/删除请求等。但是,我无法从根本上根据需要使用它来选择何时进行同步。



所以我的实际问题是:我如何强制Google Drive在需要时同步我的文件,即每次发出请求时,都会使用多个设备实现同步同一个应用程序。必须有一个答案,因为我认为这是你使用AppDataFolder的第一位的根本原因。



在此先感谢



编辑/更新:

我已经能够在Api中找到一个选项来同步使用此代码驱动含量:

  //尝试同步
Drive.DriveApi.requestSync(mGoogleApiClient).setResultCallback (新ResultCallback< com.google.android.gms.common.api.Status>(){
@覆盖
公共无效onResult(com.google.android.gms.common.api.Status状态) {
如果{
Log.e( 同步, ERROR + status.getStatusMessage())(status.getStatus()isSuccess(!)。);
}其他{
Log.e(SYNCING,SUCCESS);
//执行异步任务以列出AppFolderContents
新的AppFolderContentsAsyncTask(getActivity())。execute();
}
}
});

这适用于快速连续的3/4次尝试,但是我达到了同步限制和状态消息:


超过ERRORSync请求率限制。



<有没有什么办法可以提高请求率,因为如果我必须有一个应用程序提示用户'请稍后再试一次以同步 - 这并不是真的需要,不知道你需要等多久才能进行同步!'



解决问题的方法和我的想法(为了它的价值)

我打算使用的解决方案(通过发送已发布驱动器应用程序且无问题同步的应用程序开发者之后)是使用Drive REST Api,而不是使用较新的(Google首选的)Drive API。我试图限制'requestSync',只有当用户导航到带有Drive选项的片段(而不是每个文件事务)时。但是,这大概会解决请求同步速率限制,但它仍可能达到此限制。同样,如果多个设备正在运行该应用程序,并且链接到相同的云端硬盘帐户都同时在应用程序中同步/上传/删除文件,则可能会失去同步 - 可能是罕见的情况,但仍有可能。我不认为让用户等待文件同步是用户体验或应用设计方面可行的选择。



好奇 - 实际的云端硬盘应用可以让你刷新(requestSync?)尽可能多的次数。我在创建每个文件夹后,快速连续创建了20个文件夹,我刷新了手机上的云端硬盘应用程序,并且它全部同步了20次。 Google似乎明白同步的重要性,但选择在新的Drive API中确保这一点非常困难。如前所述,将文件上传到云存储通常是立即发生的(它是排队的,但是如果连接起来,它几乎是直接发生的)。我认为这是在移动数据/更新驱动器内容方面的代价高昂的事务,而不仅仅是查询文件的同步。但是,您可以一次添加一个文件到您的驱动器应用程序,并且几乎立即一次上传它们 - 在30秒内请求同步超过4次,然后失败并且必须冷却'一段时间。

我对编程/ Android一般都很陌生 - 只要我学到新的东西,我就会意识到自己实际上知道得有多少,所以如果他们是使用Drive API(而不是REST API)的更好解决方案,我非常欢迎它。

解决方案

DriveApi#requestSync将允许您请求从服务器到设备的同步关闭。这样,在第二台设备上,您应该能够看到从第一台设备上传的内容。但是,请求同步的呼叫受到速率限制(每设备)以避免滥用,并保证Drive API的合理数据使用量。理想情况下,您只需在需要时调用请求同步。无法提高费率限制。



关于上传完成,提交上传后,您不应该使用请求同步,因为这不会对您有所帮助同步但不起来)。实际上传将尽快发生(根据设备的连接情况以及您的应用通过 DrivePreferencesApi ,默认情况下它是不受限制的)。



如果您想知道实际上传发生的时间,可以使用 CompletionEvents 。通过这种方式,您可以通过对实际进行的更多见解来测试应用程序。


Hi I'm using the Google Drive Api to store a database using the AppDataFolder facility. I have a test app running successfully on one device. I am able to upload/update/delete/download the database file and reintegrate it into the program with no problems.

The issue I'm having is when I want to share this database with another device running the same app, then things don't work as expected. Example device A I upload the database, device B - I want to download the database but no file is found (this delay can vary greatly from seconds to hours). Reason for this - when using the Api it decides when it wants to 'sync' data, as it is queued rather than being instantaneously uploaded. So when used on one device this is not a problem because it takes either the 'synced' file from the cloud storage, or file waiting to be synced.

I have tried various things like trying to list all AppDataFolder files or retrieving metadata through a query with searchable filters before making a request to update/delete etc.. However I can't get it to work as desired fundamentally it chooses when to sync.

So my actual question is: How can I force Google Drive to sync my file when I want it to i.e. every time a request is made, so that synchronisation is achieved across multiple devices using the same app. There must be an answer as I would think this is quite a fundamental reason why you would use the AppDataFolder is the first place.

Thanks in advance

EDIT/UPDATE:

I have been able to find an option in the Api to 'sync' the drive content using this code:

            // try to sync
            Drive.DriveApi.requestSync(mGoogleApiClient).setResultCallback(new ResultCallback<com.google.android.gms.common.api.Status>() {
                @Override
                public void onResult(com.google.android.gms.common.api.Status status) {
                    if (!status.getStatus().isSuccess()) {
                        Log.e("SYNCING", "ERROR" + status.getStatusMessage());
                    } else {
                        Log.e("SYNCING", "SUCCESS");
                        // execute async task to list AppFolderContents
                        new AppFolderContentsAsyncTask(getActivity()).execute();
                    }
                }
            });

This works well for 3/4 attempts in quick succession, however I reach a syncing limit and status message:

ERRORSync request rate limit exceeded.

Is there any way to increase the request rate as this is not really desirable if I have to have a app that prompts the user 'please try again later to sync - not sure how long you'll have to wait until you can though!'

SOLUTION - OF SORTS, AND MY THOUGHTS (for what its worth)

The solution that I am going for (after emailing a app dev whose published a drive app that synchronizes without problems) is to use the Drive REST Api, rather than the newer (and Google preferred) Drive API. I tried limiting the 'requestSync' to only when the user navigated to a fragment with the Drive options (rather than every file transaction). However this would probably solve the requestSync rate limit for the most part, but it still could hit that limit. Also if multiple devices are running the app, and linked to the same Drive account were both syncing/uploading/deleting files at the same time in the app then there is a possibility of losing synchronization - maybe a rare scenario, but still possible. I don't feel that making a user wait to sync files is a viable option in terms of user experience or app design.

Curious though - The actual Drive app lets you refresh (requestSync?) as many times as you like. I created 20 folders on the web interface in quick succession, after each folder was created I refreshed the Drive app on my phone and it synced all 20 times. It seems Google understands the importance of synchronization, but chooses to make this quite difficult ensure this in their new Drive API. As already stated uploading files to the cloud storage happens usually instantly (it is queued, however if you have connectivity it happens almost straight away). I would have thought that this is the costly transaction in terms of moving data/updating drive contents, rather than just querying for synchronization of files. However you can keep adding files to your your drive app one at a time, and it uploads them one at a time almost instantly - where as you request sync more than 4 times in 30 seconds and it then fails and you have to 'cool off' for a while.

I'm very new to programming/Android in general - as soon as I learn something new, I realize how little I actually know, so if their is a better solution out there using the Drive API (rather than REST API) I'd very much welcome it.

解决方案

DriveApi#requestSync will let you request a "sync down" from the server to the device. This way, on the second device you should be able to see the content uploaded from the first device. However, the calls to request sync are rate limited (per-device) to avoid abuse and guarantee a reasonable amount of data usage by the Drive API. You should ideally call request sync only when you need to. There's no way to increase the rate limit.

Regarding upload completion, after committing an upload, you shouldn't use request sync since that won't help you (it only syncs down but not up). The actual upload will happen as soon as possible (based on the device's connectivity and the preferences specified by your app through DrivePreferencesApi, which by default are unrestricted).

If you want to know when the actual upload happened, you can use CompletionEvents. This way you can test your app with more insights on what's actually going on.

这篇关于Android Google Play / Drive Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆