【问题标题】:Google Places API for Android Error: Status{statusCode=NETWORK_ERROR, resolution=null}适用于 Android 的 Google Places API 错误:状态{statusCode=NETWORK_ERROR, resolution=null}
【发布时间】:2015-04-24 11:08:54
【问题描述】:

我在 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"/>

【问题讨论】:

  • 问题解决了吗?
  • 还没有。考虑改用服务器 API 调用。
  • 你的 mPlaceFilter == null 吗?
  • 试着让它为空。对我来说,它解决了一个问题。
  • 过滤器... 可以说目前仍在开发中github.com/googlesamples/android-play-places/issues/6

标签: android google-places-api


【解决方案1】:

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

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

这会导致状态{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),请求将导致混乱的状态错误。

【讨论】:

    【解决方案2】:
    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.
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 如果您知道为什么过滤器首先不起作用,您能分享一下原因吗?
      • 其实我也不知道原因。此外,我需要使用过滤器,但不知道热门。
      • 对我不起作用。您还有其他建议吗?
      • 我遇到了类似的问题,但不完全相同 - stackoverflow.com/questions/31640783/…任何人,请帮忙?
      【解决方案4】:

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

      <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 元数据,然后它就开始工作了。

      【讨论】:

        【解决方案5】:

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

        可选:包含一组地点类型的 AutocompleteFilter,其中 您可以使用将结果限制为一种或多种类型的地方。这 过滤器支持以下地点类型:地理编码 - 返回 只有地理编码结果,而不是企业。通常,您使用 此请求消除指定位置可能导致的结果歧义 不确定。地址 - 仅返回自动完成结果 准确的地址。通常,当您知道 用户将寻找一个完全指定的地址。成立—— 仅返回商家地点。

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              【解决方案8】:

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

              【讨论】:

                【解决方案9】:

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

                同时检查this。谷歌控制台中错误的包名导致的类似错误消息

                【讨论】:

                  猜你喜欢
                  • 2017-01-03
                  • 2017-03-19
                  • 1970-01-01
                  • 2019-08-06
                  • 2017-11-25
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-02-12
                  相关资源
                  最近更新 更多