Android Задача не завершается с помощью API Адресов - PullRequest
1 голос
/ 20 февраля 2020

Я пытаюсь в основном скопировать код из документации по API Адресов, , найденный здесь, однако, хотя код, кажется, работает, задача никогда не возвращается успешно или завершено и не выполняет этот блок кода. Это мой код, но в основном он совпадает с документами Google с некоторыми изменениями.

public void refreshLocation(TextView textView, PlacesClient placesClient) {
        // Use fields to define the data types to return.
        textView.setText("Loading...");
        List<Place.Field> placeFields = Collections.singletonList(Place.Field.NAME);

        // Use the builder to create a FindCurrentPlaceRequest.
        FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);
        textView.setText("Starting task . . .");
        Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request);
        textView.setText("Task set . . .");

        placeResponse.addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                Double likelihood = 0.0;
                String name = "";
                FindCurrentPlaceResponse response = task.getResult();
                for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
                    textView.setText("Task successful");
                    if (placeLikelihood.getLikelihood() > likelihood) {
                        likelihood = placeLikelihood.getLikelihood();
                        name = placeLikelihood.getPlace().getName();
                    }
                }
                if (likelihood != 0.0) {
                    likelihood = likelihood * 100;
                    textView.setText(String.format("You are at {}. \n {}% accurate", name, likelihood));
                } else {
                    textView.setText("Error: No Location Found.");
                }
            } else {
                Exception exception = task.getException();
                if (exception instanceof ApiException) {
                    ApiException apiException = (ApiException) exception;
                    Log.e(TAG, "Place not found: " + apiException.getStatusCode());
                }
            }
        });


    }

Я что-то упустил в отношении запуска задачи? Есть ли какой-нибудь способ запустить его или выполнить? Это то, что может быть решено с помощью AsyncTask, хотя Документы Google не используют его?

...