Google商家信息自动完成API在Instant Apps配置中出现API_NOT_CONNECTED错误

时间:2017-11-30 18:47:36

标签: android kotlin google-play-services google-places-api android-instant-apps

我正在尝试将Google Places AutoComplete API添加到我的Android Instant Apps项目中。

我按照this sample来实施API。

我的dialog.cancel(); 课程:

PlacesAdapter

当我在package net.epictimes.uvindex.autocomplete import android.content.Context import android.graphics.Typeface import android.text.style.StyleSpan import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter import android.widget.Filter import android.widget.Filterable import android.widget.TextView import com.google.android.gms.common.data.DataBufferUtils import com.google.android.gms.location.places.AutocompletePrediction import com.google.android.gms.location.places.GeoDataClient import com.google.android.gms.maps.model.LatLng import com.google.android.gms.maps.model.LatLngBounds import com.google.android.gms.tasks.RuntimeExecutionException import com.google.android.gms.tasks.Tasks import timber.log.Timber import java.util.concurrent.ExecutionException import java.util.concurrent.TimeUnit import java.util.concurrent.TimeoutException class PlacesAdapter(context: Context, private val geoDataClient: GeoDataClient) : ArrayAdapter<AutocompletePrediction>(context, android.R.layout.simple_expandable_list_item_2, android.R.id.text1), Filterable { private val resultList = mutableListOf<AutocompletePrediction>() private val styleBold = StyleSpan(Typeface.BOLD) override fun getCount(): Int = resultList.size override fun getItem(position: Int): AutocompletePrediction = resultList[position] override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { val row = super.getView(position, convertView, parent) // Sets the primary and secondary text for a row. // Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain // styling based on the given CharacterStyle. val item = getItem(position) val textView1 = row.findViewById<View>(android.R.id.text1) as TextView val textView2 = row.findViewById<View>(android.R.id.text2) as TextView textView1.text = item.getPrimaryText(styleBold) textView2.text = item.getSecondaryText(styleBold) return row } override fun getFilter(): Filter { return object : Filter() { override fun performFiltering(constraint: CharSequence?): FilterResults { val results = FilterResults() // We need a separate list to store the results, since // this is run asynchronously. var filterData: ArrayList<AutocompletePrediction>? = ArrayList() // Skip the autocomplete query if no constraints are given. if (constraint != null) { // Query the autocomplete API for the (constraint) search string. filterData = getAutocomplete(constraint) } results.values = filterData if (filterData != null) { results.count = filterData.size } else { results.count = 0 } return results } override fun publishResults(constraint: CharSequence?, results: FilterResults?) { if (results != null && results.count > 0) { // The API returned at least one result, update the data. resultList.clear() resultList.addAll(results.values as ArrayList<AutocompletePrediction>) } notifyDataSetChanged() } } } /** * Submits an autocomplete query to the Places Geo Data Autocomplete API. * Results are returned as frozen AutocompletePrediction objects, ready to be cached. * Returns an empty list if no results were found. * Returns null if the API client is not available or the query did not complete * successfully. * This method MUST be called off the main UI thread, as it will block until data is returned * from the API, which may include a network request. * * @param constraint Autocomplete query string * @return Results from the autocomplete API or null if the query was not successful. * @see GeoDataClient.getAutocompletePredictions * @see AutocompletePrediction.freeze */ private fun getAutocomplete(constraint: CharSequence): ArrayList<AutocompletePrediction>? { Timber.d("Starting autocomplete query for: " + constraint) val latLngBounds = LatLngBounds(LatLng(-34.041458, 150.790100), LatLng(-33.682247, 151.383362)) // Submit the query to the autocomplete API and retrieve a PendingResult that will // contain the results when the query completes. val results = geoDataClient.getAutocompletePredictions(constraint.toString(), latLngBounds, null) // This method should have been called off the main UI thread. Block and wait for at most // 60s for a result from the API. try { Tasks.await(results, 60, TimeUnit.SECONDS) } catch (e: ExecutionException) { Timber.e(e) } catch (e: InterruptedException) { Timber.e(e) } catch (e: TimeoutException) { Timber.e(e) } Timber.d("Query completed. Received " + results.result.count + " predictions.") return try { // Freeze the results immutable representation that can be stored safely. DataBufferUtils.freezeAndClose<AutocompletePrediction, AutocompletePrediction>(results.result) } catch (e: RuntimeExecutionException) { // If the query did not complete successfully return null Timber.e("Error getting autocomplete prediction API call", e) null } } } 配置中运行项目时,它可以正常运行

但是当我在app配置中运行它时会抛出这个:

instantapp
docs

D/PlacesAdapter: Starting autocomplete query for: is E/GmsClient: unable to connect to service: com.google.android.gms.location.places.GeoDataApi on com.google.android.gms E/PlacesAdapter: java.util.concurrent.ExecutionException: com.google.android.gms.common.api.ApiException: 17: API_NOT_CONNECTED at com.google.android.gms.tasks.Tasks.zzc(Unknown Source:17) at com.google.android.gms.tasks.Tasks.await(Unknown Source:53) at net.epictimes.uvindex.autocomplete.PlacesAdapter.getAutocomplete(PlacesAdapter.kt:116) at net.epictimes.uvindex.autocomplete.PlacesAdapter.access$getAutocomplete(PlacesAdapter.kt:25) at net.epictimes.uvindex.autocomplete.PlacesAdapter$getFilter$1.performFiltering(PlacesAdapter.kt:64) at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.os.HandlerThread.run(HandlerThread.java:65) Caused by: com.google.android.gms.common.api.ApiException: 17: API_NOT_CONNECTED at com.google.android.gms.common.internal.zzb.zzz(Unknown Source:14) at com.google.android.gms.common.internal.zzbk.zzaa(Unknown Source:0) at com.google.android.gms.common.internal.zzbl.zzs(Unknown Source:32) at com.google.android.gms.common.api.internal.zzs.zzc(Unknown Source:46) at com.google.android.gms.common.api.internal.zzs.setResult(Unknown Source:42) at com.google.android.gms.common.api.internal.zzm.zzv(Unknown Source:17) at com.google.android.gms.common.api.internal.zzc.zzt(Unknown Source:2) at com.google.android.gms.common.api.internal.zzbr.zzx(Unknown Source:27) at com.google.android.gms.common.api.internal.zzbp.handleMessage(Unknown Source:386) at android.os.Handler.dispatchMessage(Handler.java:101) at android.os.Looper.loop(Looper.java:164)  at android.os.HandlerThread.run(HandlerThread.java:65)  W/Filter: An exception occured during performFiltering()! com.google.android.gms.tasks.RuntimeExecutionException: com.google.android.gms.common.api.ApiException: 17: API_NOT_CONNECTED at com.google.android.gms.tasks.zzn.getResult(Unknown Source:14) at net.epictimes.uvindex.autocomplete.PlacesAdapter.getAutocomplete(PlacesAdapter.kt:123) at net.epictimes.uvindex.autocomplete.PlacesAdapter.access$getAutocomplete(PlacesAdapter.kt:25) at net.epictimes.uvindex.autocomplete.PlacesAdapter$getFilter$1.performFiltering(PlacesAdapter.kt:64) at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.os.HandlerThread.run(HandlerThread.java:65) Caused by: com.google.android.gms.common.api.ApiException: 17: API_NOT_CONNECTED at com.google.android.gms.common.internal.zzb.zzz(Unknown Source:14) at com.google.android.gms.common.internal.zzbk.zzaa(Unknown Source:0) at com.google.android.gms.common.internal.zzbl.zzs(Unknown Source:32) at com.google.android.gms.common.api.internal.zzs.zzc(Unknown Source:46) at com.google.android.gms.common.api.internal.zzs.setResult(Unknown Source:42) at com.google.android.gms.common.api.internal.zzm.zzv(Unknown Source:17) at com.google.android.gms.common.api.internal.zzc.zzt(Unknown Source:2) at com.google.android.gms.common.api.internal.zzbr.zzx(Unknown Source:27) at com.google.android.gms.common.api.internal.zzbp.handleMessage(Unknown Source:386) at android.os.Handler.dispatchMessage(Handler.java:101) at android.os.Looper.loop(Looper.java:164)  at android.os.HandlerThread.run(HandlerThread.java:65)  错误代码的解释:

  

客户端尝试从未通过的API调用方法   连接。可能的原因包括:

     
      
  • 以前无法使用可解析的错误连接API,但用户拒绝了解决方案。
  •   
  • 该设备不支持GmsCore。
  •   
  • 特定API无法在此设备上连接。
  •   

我在模拟器上更新了Google Play服务(它的11.7.46(470-175121617)BTW),但它没有改变任何内容。

我将我的Google Play服务依赖项放入API_NOT_CONNECTED模块。我检查了合并的清单文件,它们都有Google Play API密钥。

Android Developers site他们在支持的 API列表中列出了Google Places API。

我在其他功能模块中使用Google Location API,它在baseapp配置中都有效。

那我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我决定使用GoogleApiClient连接API:

GoogleApiClient.Builder(this)
                .enableAutoManage(this, this)
                .addApi(Places.GEO_DATA_API)
                .build()
                .connect()

但在API_UNAVAILABLE配置中,连接尝试失败,状态代码为instantapp

docs中,API_UNAVAILABLE表示该API不适用于即时应用:

  

调试使用Google Play服务库的即时应用时   除了上面列表中包含的那些,你可能会看到   调试输出中的API_UNAVAILABLE错误。 API_UNAVAILABLE   错误表示该库尚未适应使用   Android即时应用。

所以我删除了地方自动完成功能,并继续使用类似geocoding的内容。