简体   繁体   中英

Error getting autocomplete prediction API call: com.google.android.gms.common.api.ApiException: 9003: PLACES_API_ACCESS_NOT_CONFIGURED

My placeautocomplete adapter is not working, I have added the api key, billing is also approved. Whenever I click to search a place it gives me an error of ApiException:9003 and the logcat points to the "PlaceAutocompleteAdapter.java" shown below.

public class PlaceAutocompleteAdapter extends ArrayAdapter implements Filterable {

private static final String TAG = "PlaceAutoComplete";
private static final CharacterStyle STYLE_BOLD = new StyleSpan(Typeface.BOLD);

private ArrayList<AutocompletePrediction> mResultList;

private GeoDataClient mGeoDataClient;


private LatLngBounds mBounds;

private AutocompleteFilter mPlaceFilter;


public PlaceAutocompleteAdapter(Context context, GeoDataClient geoDataClient,
                                LatLngBounds bounds, AutocompleteFilter filter) {
    super(context, android.R.layout.simple_expandable_list_item_2, android.R.id.text1);
    mGeoDataClient = geoDataClient;
    mBounds = bounds;
    mPlaceFilter = filter;
}

public void setBounds(LatLngBounds bounds) {
    mBounds = bounds;
}

@Override
public int getCount() {
    return mResultList.size();
}

@Override
public AutocompletePrediction getItem(int position) {
    return mResultList.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);

    // Sets the primary and secondary text for a row.
    // Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain
    // styling based on the given CharacterStyle.

    AutocompletePrediction item = getItem(position);

    TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
    TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
    textView1.setText(item.getPrimaryText(STYLE_BOLD));
    textView2.setText(item.getSecondaryText(STYLE_BOLD));

    return row;
}

@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            // We need a separate list to store the results, since
            // this is run asynchronously.
            ArrayList<AutocompletePrediction> filterData = new ArrayList<>();

            // Skip the autocomplete query if no constraints are given.
            if (constraint != null) {
                // Query the autocomplete API for the (constraint) search string.
                filterData = getAutocomplete(constraint);
            }

            results.values = filterData;
            if (filterData != null) {
                results.count = filterData.size();
            } else {
                results.count = 0;
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            if (results != null && results.count > 0) {
                // The API returned at least one result, update the data.
                mResultList = (ArrayList<AutocompletePrediction>) results.values;
                notifyDataSetChanged();
            } else {
                // The API did not return any results, invalidate the data set.
                notifyDataSetInvalidated();
            }
        }

        @Override
        public CharSequence convertResultToString(Object resultValue) {
            // Override this method to display a readable result in the AutocompleteTextView
            // when clicked.
            if (resultValue instanceof AutocompletePrediction) {
                return ((AutocompletePrediction) resultValue).getFullText(null);
            } else {
                return super.convertResultToString(resultValue);
            }
        }
    };
}

private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
    Log.i(TAG, "Starting autocomplete query for: " + constraint);

    // Submit the query to the autocomplete API and retrieve a PendingResult that will
    // contain the results when the query completes.
    Task<AutocompletePredictionBufferResponse> results =
            mGeoDataClient.getAutocompletePredictions(constraint.toString(), mBounds,
                    mPlaceFilter);

    // This method should have been called off the main UI thread. Block and wait for at most
    // 60s for a result from the API.
    try {
        Tasks.await(results, 60, TimeUnit.SECONDS);
    } catch (ExecutionException | InterruptedException | TimeoutException e) {
        e.printStackTrace();
    }

    try {
        AutocompletePredictionBufferResponse autocompletePredictions = results.getResult();

        Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
                + " predictions.");

        // Freeze the results immutable representation that can be stored safely.
        return DataBufferUtils.freezeAndClose(autocompletePredictions);
    } catch (RuntimeExecutionException e) {
        // If the query did not complete successfully return null
        Toast.makeText(getContext(), "Error contacting API: " + e.toString(),
                Toast.LENGTH_SHORT).show();
        Log.e(TAG, "Error getting autocomplete prediction API call", e);
        return null;
    }
}

}

Maybe you forgot to ENABLE the API in Google Api Console?

Please see:

https://support.google.com/googleapi/answer/6158841?hl=en

According to what I see, you might also be using the deprecated version of the Google Services SDK, which has been decommissioned in July 2019.

Please see here for more info:

https://developers.google.com/places/android-sdk/client-migration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM