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

Sha*_*aik 9 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;
}
Run Code Online (Sandbox Code Playgroud)

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"/>
Run Code Online (Sandbox Code Playgroud)

cip*_*ian 7

Himanshu是对的; 它发生在我身上,我想按城市过滤并添加:

AutocompleteFilter.create(Arrays.asList(
    Place.TYPE_LOCALITY, Place.TYPE_ADMINISTRATIVE_AREA_LEVEL_3));
Run Code Online (Sandbox Code Playgroud)

这导致状态{statusCode = NETWORK_ERROR,resolution = null}

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

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

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


小智 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.
Run Code Online (Sandbox Code Playgroud)


Eld*_*tov 4

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