Google Places API for Android Error: Status{statusCode=NETWORK_ERROR, resolution=null}

Google Places API for Android Error: Status{statusCode=NETWORK_ERROR, resolution=null}

我已经在 google 开发者控制台下创建了一个项目,并且想为 Android 使用 Google Places API。我已启用 API 并按照 Google 开发者网站 (https://developers.google.com/places/android/start) 上的指南集成到我的 android 应用程序中。 API 出现此错误,经过数小时(和数天)的调试和互联网搜索后,我无法缩小问题范围。对于出现此错误的原因,我们将不胜感激。

错误:Status{statusCode=NETWORK_ERROR, resolution=null}

以下是 google 示例代码中调用“地点自动完成”的函数。

private ArrayList<PlaceAutocomplete> getAutocomplete(CharSequence constraint) {
    if (mGoogleApiClient != null) {
        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.
        PendingResult<AutocompletePredictionBuffer> results =
                Places.GeoDataApi
                        .getAutocompletePredictions(mGoogleApiClient, 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.
        AutocompletePredictionBuffer autocompletePredictions = results
                .await(60, TimeUnit.SECONDS);

        // Confirm that the query completed successfully, otherwise return null
        final Status status = autocompletePredictions.getStatus();
        if (!status.isSuccess()) {
            Toast.makeText(getContext(), "Error contacting API: " + status.toString(),
                    Toast.LENGTH_SHORT).show();
            Log.e(TAG, "Error getting autocomplete prediction API call: " + status.toString());
            autocompletePredictions.release();
            return null;
        }

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

        // Copy the results into our own data structure, because we can't hold onto the buffer.
        // AutocompletePrediction objects encapsulate the API response (place ID and description).

        Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
        ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
        while (iterator.hasNext()) {
            AutocompletePrediction prediction = iterator.next();
            // Get the details of this prediction and copy it into a new PlaceAutocomplete object.
            resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
                    prediction.getDescription()));
        }

        // Release the buffer now that all data has been copied.
        autocompletePredictions.release();

        return resultList;
    }
    Log.e(TAG, "Google API client is not connected for autocomplete query.");
    return null;
}

AndroidManifiest.xml 权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>

我认为你应该 post 你的清单权限。

同时勾选 this。 google console

包名错误引起的类似错误信息

如果您想获得任何类型的预测,您可以在 getAutocompletePredictions 方法中使用 null 而不是 AutocompleteFilter 属性。

您的代码可能存在问题,因为您在 AutoCompleteFilter 中使用的地点类型不是机构、地址和地理代码。目前只支持这三个。 Google 文档中的代码部分。

Optional: An AutocompleteFilter containing a set of place types, which you can use to restrict the results to one or more types of place. The following place types are supported in the filter: geocode – Returns only geocoding results, rather than businesses. Generally, you use this request to disambiguate results where the location specified may be indeterminate. address – Returns only autocomplete results with a precise address. Generally, you use this request when you know the user will be looking for a fully specified address. establishment – Returns only places that are businesses.

希曼舒说得对;它也发生在我身上我想按城市过滤并添加:

AutocompleteFilter.create(Arrays.asList(
    Place.TYPE_LOCALITY, Place.TYPE_ADMINISTRATIVE_AREA_LEVEL_3));

这导致 Status{statusCode=NETWORK_ERROR, resolution=null}

并非 Place 中的所有常量都可用于使用 AutocompleteFilter 过滤,只有两个在 Android 上可用:Place.TYPE_GEOCODEPlace.TYPE_ESTABLISHMENT('address' 出现在文档中但在 Place)

中不是常量

仔细阅读这里:https://developers.google.com/places/supported_types#table3

如果使用任何其他常量(例如 Place.TYPE_LOCALITY),请求将导致混乱的状态错误。

对我来说,我已经为地图和 Geo 提供了元标记

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="*********************"/>
<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="**********************" />

然后我删除了 Map meta 然后它开始工作了。

Please ensure the folowing.

1. The package name given in console registration page is same as that of actvity/fragment we are using. ie; if are using this auto search button inside com.example.rajeesh.UI.fragements.SearchFragment, 
then inside console registration page  package name should be 
com.example.rajeesh.UI.fragements
This is different from the package name we given in manifest file.

2. Inside manifest please  change 
  <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="@string/google_maps_api_key" />
to

      <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="@string/google_maps_api_key" />


3. Enable Google Places API for Android in console page.

地点 API 自 2019 年 1 月以来发生了变化: 尝试更改实现: 实施'com.google.android.gms:play-services-places:16.0.0' 到 '实现'com.google.android.libraries.places:places-compat:2.1.0'

对于Google地点API 你们需要从 Google Cloud 启用计费系统。因为现在Google不再提供免费名额API。

根据 this地点选取器不再可用