Google Translate API sometimes takes very long time to initialize
Asked Answered
L

1

11

To initialize the Google Translate API, it must be done in a thread. Most of the time it only takes 2 seconds. However, 1 out of every 5 times, it takes anywhere from 20 seconds to 3 minutes (Unacceptable).

AppCompatActivity where I Initialize Google Translate API

      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

I also have the latest version in my gradle (module):

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

Note

It used to work instantly, this error is very recent. I'm not sure why this is occurring out of nowhere.

Lenna answered 8/4, 2017 at 1:43 Comment(3)
How are you profiling which specific blocks of code take how much time to run? Is it just that your whole request handler takes that long to run?Footlights
Yes, the whole AsynctTask Execution takes that long with the Translate initialization inside of DoInBackground, the Logs in the DoInBackground and and Post Execute track how long that one line of code taks. @FootlightsLenna
Given the time profile of the event, it could simply be a case of a degradation in the connection between the client and the server. Is this still occurring? And does it occur consistently, or just most of the time / some of the time? And how does the client connect to the network? Has this appeared on multiple clients?Footlights
I
1

Try replacing

asyncTask.execute();

with

asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

asyncTask.execute() Execute method runs in serial mode, if any other async task has been executed before it & that task is still running, then it will wait for other async task to finish.

Where as, executeOnExecutor will run asynctasks in parallel

Incapacity answered 10/4, 2017 at 6:49 Comment(2)
Already tried this, problem is still there, it might be Google's server side's fault.Lenna
@grant Maybe it is really Google's server side's fault because I use the Google Nearby API which needs to connect to Google in order to work and a few days ago it caused the Google Play Service to crash in about 95% of the times I tried my code. Today my code only produces a crash of the API in 10% of the times. And my code did't change.Palla

© 2022 - 2024 — McMap. All rights reserved.