10

I have created a project under google developer console and wanted to use Google Places API for Android. I have enabled the API and integrated into my android application as per the guidelines on Google developer website (https://developers.google.com/places/android/start). The API is giving this error and after hours (and days) of debugging and searching the internet I couldn't narrow down the problem. Any leads would be appreciated about why this error is coming up.

Error: Status{statusCode=NETWORK_ERROR, resolution=null}

The following is the function from the google sample code where Places Auto Complete is invoked.

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 permissions

<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"/>
Sharief Shaik
  • 1,064
  • 5
  • 12
  • 26

9 Answers9

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.

ciprian
  • 151
  • 1
  • 5
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.
rajeesh
  • 937
  • 10
  • 11
4

You can put null instead of AutocompleteFilter property in getAutocompletePredictions method if you want to get predictions of any types.

dreambit.io dreambitio
  • 1,892
  • 1
  • 15
  • 24
2

For me, I had given meta tag for both Map and 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="**********************" />

then I removed Map meta and then it started working.

T_V
  • 17,440
  • 6
  • 36
  • 48
0

Problem with your code could be that you are using place type other than establishment, address and geo code in your AutoCompleteFilter. As of now only these three are supported. Code parts from Google's docs.

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.

Himanshu Virmani
  • 2,450
  • 1
  • 24
  • 34
0

Places API has been changed since January 2019: Try changing the implementation: implementation 'com.google.android.gms:play-services-places:16.0.0' to 'implementation 'com.google.android.libraries.places:places-compat:2.1.0'

0

For Google Place API You guys need to enable billing system from Google Cloud . Because now Google no more providing free places API.

Rakesh Jha
  • 279
  • 2
  • 7
0

According to this, Place Picker is no longer available

yshahak
  • 4,996
  • 1
  • 31
  • 37
-1

I think you should post your manifest permissions.

Also check this. Similar error message caused by wrong package name in google console

Community
  • 1
  • 1
X3Btel
  • 1,408
  • 1
  • 13
  • 21