【问题标题】:Google Translate API sometimes takes very long time to initializeGoogle Translate API 有时需要很长时间才能初始化
【发布时间】:2017-09-03 11:57:03
【问题描述】:

要初始化 Google Translate API,必须在线程中完成。大多数时候只需要 2 秒。但是,每 5 次中有 1 次需要 20 秒到 3 分钟(不可接受)。

我在其中初始化 Google Translate API 的 AppCompatActivity

      AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {

        @Override
        public void onPostExecute (Void aVoid) {

            Log.i("APP", "finished initializing");

        }

        @Override
        protected Void doInBackground(Void... voids) {
            Log.i("APP", "started initializing");
            translate2 = TranslateOptions.newBuilder().setApiKey(MY_API_KEY).build().getService();

            return null;
        }

    };
    asyncTask.execute();

Gradle

我的 gradle(模块)中也有最新版本:

 compile ('com.google.apis:google-api-services-translate:v2-rev49-1.22.0')

注意

它曾经立即工作,这个错误是最近的。我不确定为什么会突然发生这种情况。

【问题讨论】:

  • 您如何分析哪些特定代码块需要多长时间才能运行?只是你的整个请求处理程序需要这么长时间才能运行吗?
  • 是的,整个 AsynctTask 执行需要很长时间,因为 DoInBackground 内部的 Translate 初始化,DoInBackground 中的日志和 Post Execute 跟踪一行代码需要多长时间。 @尼克
  • 鉴于事件的时间概况,这可能只是客户端和服务器之间的连接性能下降的情况。这还在发生吗?它是否始终如一地发生,或者只是大部分时间/某些时间?客户端如何连接到网络?这是否出现在多个客户端上?

标签: android google-api google-translate


【解决方案1】:

尝试替换

asyncTask.execute();

asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

asyncTask.execute() Execute 方法以串行模式运行,如果在它之前执行了任何其他异步任务并且该任务仍在运行,那么它将等待其他异步任务完成。

在哪里, executeOnExecutor 将并行运行异步任务

【讨论】:

  • 已经试过了,问题依旧,可能是谷歌服务器端的问题。
  • @grant 也许这真的是谷歌服务器端的错,因为我使用谷歌附近的 API 需要连接到谷歌才能工作,几天前它导致谷歌播放服务崩溃了95% 的时间我都尝试过我的代码。今天,我的代码只有 10% 的时间会导致 API 崩溃。我的代码没有改变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
相关资源
最近更新 更多