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

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

问题描述

我正在使用 Google Drive Api 使用 AppDataFolder 工具存储数据库.我在一台设备上成功运行了一个测试应用程序.我可以上传/更新/删除/下载数据库文件并将其重新集成到程序中,没有任何问题.

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.

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

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.

在发出更新/删除请求等之前,我尝试了各种尝试,例如尝试列出所有 AppDataFolder 文件或通过具有可搜索过滤器的查询检索元数据.同步.

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.

所以我的实际问题是:如何强制 Google Drive 在我需要时同步我的文件,即每次发出请求时,以便使用同一应用程序在多个设备上实现同步.必须有一个答案,因为我认为这是您首先使用 AppDataFolder 的一个基本原因.

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.

提前致谢

编辑/更新:

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

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

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

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

超出 ERRORSync 请求速率限制.

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!'

解决方案 - 各种各样的,以及我的想法(它的价值)

我想要的解决方案(在向应用开发者发送电子邮件后,该应用开发者发布了一个同步无问题的驱动器应用程序)是使用 Drive REST Api,而不是更新的(和 Google 首选的)Drive API.我尝试将requestSync"限制为仅当用户使用 Drive 选项导航到片段时(而不是每个文件事务).然而,这可能会在很大程度上解决 requestSync 速率限制,但它仍然可能达到该限制.此外,如果多个设备正在运行该应用程序,并且链接到同一个 Drive 帐户同时在该应用程序中同步/上传/删除文件,则有可能失去同步——这可能是一种罕见的情况,但仍有可能.我不认为让用户等待同步文件在用户体验或应用设计方面是一个可行的选择.

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.

虽然很好奇 - 实际的 Drive 应用程序可让您根据需要多次刷新(requestSync?).我快速连续地在 Web 界面上创建了 20 个文件夹,创建每个文件夹后,我刷新了手机上的 Drive 应用程序,它同步了所有 20 次.谷歌似乎了解同步的重要性,但选择在他们的新 Drive API 中确保这一点非常困难.如前所述,将文件上传到云存储通常会立即发生(它会排队,但是如果您有连接,它几乎会立即发生).我原以为这是移动数据/更新驱动器内容方面的昂贵事务,而不仅仅是查询文件同步.但是,您可以一次一个地将文件添加到您的驱动器应用程序中,并且它几乎立即一次上传一个文件 - 当您在 30 秒内请求同步 4 次以上,然后它失败,您必须冷静下来"' 一会儿.

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.

总的来说,我对编程/Android 非常陌生 - 一旦我学习了一些新东西,我就会意识到我实际上知道的很少,所以如果他们是使用 Drive API(而不是 REST API)的更好的解决方案我非常欢迎它.

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 将让您请求从服务器到设备的同步".这样,在第二台设备上,您应该能够看到从第一台设备上传的内容.但是,请求同步的调用是有速率限制的(每个设备),以避免滥用并保证 Drive API 使用合理的数据量.理想情况下,您应该仅在需要时调用请求同步.没有办法提高速率限制.

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.

关于上传完成,在提交上传后,您不应该使用请求同步,因为这对您没有帮助(它只同步而不同步).实际上传将尽快发生(基于设备的连接性和您的应用通过 DrivePreferencesApi,默认情况下不受限制.

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天全站免登陆