Google Places API for Android错误:状态{statusCode = NETWORK_ERROR,resolution = null}

时间:2015-04-24 11:08:54

标签: android google-places-api

我在google开发者控制台下创建了一个项目,并希望使用适用于Android的Google Places API。我已根据Google开发人员网站(https://developers.google.com/places/android/start)上的指南启用了API并将其集成到我的Android应用程序中。 API正在提供此错误,在调试和搜索互联网的几小时(和几天)后,我无法缩小问题范围。任何线索都会被理解为什么会出现这个错误。

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

以下是调用Places Auto Complete的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"/>

9 个答案:

答案 0 :(得分:7)

Himanshu is right; it happens to me as well I wanted to filter by cities and added:

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

this result in Status{statusCode=NETWORK_ERROR, resolution=null}

Not all constant from Place are available for filtering with AutocompleteFilter, only two are available on Android: Place.TYPE_GEOCODE and Place.TYPE_ESTABLISHMENT ('address' appear in documentation but not as constant in Place)

Read carefully here:https://developers.google.com/places/supported_types#table3

If any other constant (e.g. Place.TYPE_LOCALITY) are used the request will result in a confusing status error.

答案 1 :(得分:5)

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.

答案 2 :(得分:2)

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

答案 3 :(得分:2)

对我来说,我已经为Map和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,然后开始工作。

答案 4 :(得分:0)

您的代码问题可能是您在AutoCompleteFilter中使用的地点类型不是建立,地址和地理位置代码。截至目前,只支持这三个。来自Google文档的代码部分。

  

可选:包含一组场所类型的AutocompleteFilter   您可以使用将结果限制为一种或多种类型的地方。该   过滤器支持以下地点类型:geocode - 返回   只有地理编码结果,而不是企业。一般来说,你使用   此请求消除指定位置的结果的歧义   不确定。 address - 仅返回自动完成结果   准确的地址。通常,您在知道时使用此请求   用户将寻找一个完全指定的地址。建立 -   仅返回作为商家的地方。

答案 5 :(得分:0)

Places API自2019年1月以来已更改: 尝试更改实现: 实现'com.google.android.gms:play-services-places:16.0.0' 至 '实施'com.google.android.libraries.places:places-compat:2.1.0'

答案 6 :(得分:0)

对于Google Place API 你们需要从Google Cloud启用计费系统。因为现在Google不再提供免费的Places API。

答案 7 :(得分:0)

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

答案 8 :(得分:-1)

我认为您应该发布您的清单权限。

同时检查this。由谷歌控制台错误的包名称引起的类似错误消息