2015-04-24 13 views
6

Ho creato un progetto sotto la console per sviluppatori di Google e ho voluto utilizzare l'API di Google Places per Android. Ho abilitato l'API e integrato nella mia applicazione Android come da linee guida sul sito web degli sviluppatori di Google (https://developers.google.com/places/android/start). L'API sta dando questo errore e dopo ore (e giorni) di debug e ricerche su Internet non ho potuto restringere il problema. Qualsiasi vantaggio sarebbe apprezzato sul motivo per cui questo errore è in arrivo.API Google Places per Android Errore: stato {statusCode = NETWORK_ERROR, risoluzione = null}

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

Quello che segue è la funzione del codice di esempio di Google in cui viene richiamato Complete posti auto.

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; 
} 

permessi 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"/> 
+0

Hai risolto il problema? –

+0

Non ancora. Pensando invece di utilizzare la chiamata all'API del server. –

+0

Il tuo mPlaceFilter == null? –

risposta

2

È possibile inserire null anziché la proprietà AutocompleteFilter nel metodo getAutocompletePredictions se si desidera ottenere previsioni di qualsiasi tipo.

+1

Puoi condividere il motivo se sai perché un filtro non ha funzionato al primo posto? –

+1

In realtà non conosco la ragione. Inoltre ho bisogno di usare i filtri ma non so come. –

+0

non ha funzionato per me. Hai altri suggerimenti? – sk1pro99

-1

penso che si dovrebbe inviare le autorizzazioni manifeste.

Controllare anche this. Messaggio di errore simile causato dal nome del pacchetto errato nella console di google

+0

Domanda aggiornata con permessi manifest. –

0

Il problema con il codice potrebbe essere che si sta utilizzando un tipo di luogo diverso da stabilimento, indirizzo e codice geografico nel filtro di completamento automatico. A partire da ora solo questi tre sono supportati. Parti di codice dai documenti di 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.

6

Himanshu ha ragione; succede anche a me volevo filtrare per le città e ha aggiunto:

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

questo risultato in stato {statusCode = NETWORK_ERROR, risoluzione = null}

Non

tutto costante da Place sono disponibili per il filtraggio con AutocompleteFilter , solo due sono disponibili su Android: Place.TYPE_GEOCODE e Place.TYPE_ESTABLISHMENT ('indirizzo' compare nella documentazione, ma non come costante nella Place)

Leggere attentamente qui: https://developers.google.com/places/supported_types#table3

Se qualsiasi altra costante (ad es. Place.TYPE_LOCALITY) la richiesta comporterà un errore di stato confuso.

0

Per me, avevo assegnato il meta tag sia a Mappa che a 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="**********************" /> 

poi ho rimosso Mappa meta e poi ha iniziato a lavorare.

0
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.