我想谷歌的地方自动完成API添加到我的Android应用程序的即时项目。
我跟着这个样本来实现API。
我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
配置它抛出这样的:
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_NOT_CONNECTED
错误代码的在explation 文档 :
客户端尝试从连接失败的API调用的方法。 可能的原因包括:
- 该API之前未能与解析错误连接,但用户拒绝的分辨率。
- 该设备不支持GmsCore。
- 具体的API不能在此设备上的连接。
我更新了谷歌播放在模拟器上服务(这是46年7月11日(470-175121617)BTW),但它并没有改变点儿。
我把我的谷歌游戏服务的依赖关系到base
模块。 我查了合并的清单文件,它们都具有谷歌播放API密钥。
在Android开发者网站中,他们列出谷歌的地方API在支持的API列表。
我使用谷歌定位API的其他功能模块,它工作在两种app
和instantapp
配置。
所以我在做什么错在这里?