获取自动完成预测API调用时出错:com.google.android.gms.common.api.ApiException:9003:PLACES_API_ACCESS_NOT_CONFIGURED

时间:2019-08-24 21:40:07

标签: java firebase placeautocompletefragment

我的placeautocomplete适配器无法正常工作,我添加了api密钥,帐单也已批准。每当我单击搜索位置时,都会给我ApiException:9003错误,并且logcat指向如下所示的“ PlaceAutocompleteAdapter.java”。

公共类PlaceAutocompleteAdapter 扩展ArrayAdapter实现了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;
    }
}

}

1 个答案:

答案 0 :(得分:0)

也许您忘记了在Google Api Console中启用API?

请参阅:

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

根据我所见,您可能还使用了不推荐使用的Google Services SDK,该版本已于2019年7月停用。

请在此处查看更多信息:

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