android - 使用新的 Places api 自动完成预测
问题描述
我正在尝试更新我的自动完成适配器,以便使用适用于 Android 的新 Google Places api。已弃用的 api 一切正常,但是在更新到新的 api 后,我遇到了一个奇怪的异常。这是我的代码:
private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
final ArrayList<AutocompletePrediction> resultList = new ArrayList<>();
RectangularBounds bounds = RectangularBounds.newInstance(mBounds);
FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder()
//.setLocationBias(bounds)
//.setLocationRestriction(bounds)
//.setCountry("es")
//.setTypeFilter(TypeFilter.ADDRESS)
.setSessionToken(mAutocompleteSessionToken)
.setQuery(constraint.toString())
.build();
Task<FindAutocompletePredictionsResponse> autocompletePredictions = this.mPlacesClient.findAutocompletePredictions(request);
try {
Tasks.await(autocompletePredictions, 60, TimeUnit.SECONDS);
}catch (ExecutionException | TimeoutException e) {
Log.d(TAG, e.getLocalizedMessage());
}catch (InterruptedException ie){
Log.d(TAG, ie.getLocalizedMessage());
Thread.currentThread().interrupt();
}
if (autocompletePredictions.isSuccessful()) {
FindAutocompletePredictionsResponse findAutocompletePredictionsResponse = autocompletePredictions.getResult();
if (findAutocompletePredictionsResponse != null) {
resultList.addAll(findAutocompletePredictionsResponse.getAutocompletePredictions());
}
}
return resultList;
}
当代码到达 Tasks.await 行时,抛出如下异常:
2019-06-06 16:40:34.374 15915-16101/com.my.new.app D/PlaceAutocompleteAdaptr: com.google.android.gms.common.api.ApiException: 7: javax.net.ssl.SSLPeerUnverifiedException: Hostname maps.googleapis.com not verified:
certificate: sha1/um1YfTYENrO2PbPGj5v0Jra2BlQ=
DN: CN=*.googleapis.com,O=Google LLC,L=Mountain View,ST=California,C=US
subjectAltNames: [*.googleapis.com, *.clients6.google.ae, *.clients6.google.at, *.clients6.google.be, *.clients6.google.ca, *.clients6.google.ch, *.clients6.google.cl, *.clients6.google.co.id, *.clients6.google.co.il, *.clients6.google.co.in, *.clients6.google.co.jp, *.clients6.google.co.kr, *.clients6.google.co.nz, *.clients6.google.co.uk, *.clients6.google.co.ve, *.clients6.google.co.za, *.clients6.google.com, *.clients6.google.com.ar, *.clients6.google.com.au, *.clients6.google.com.br, *.clients6.google.com.co, *.clients6.google.com.eg, *.clients6.google.com.kw, *.clients6.google.com.mx, *.clients6.google.com.om, *.clients6.google.com.pe, *.clients6.google.com.ph, *.clients6.google.com.qa, *.clients6.google.com.sa, *.clients6.google.com.sg, *.clients6.google.com.tr, *.clients6.google.com.tw, *.clients6.google.com.ua, *.clients6.google.com.vn, *.clients6.google.cz, *.clients6.google.de, *.clients6.google.dk, *.clients6.google.es, *.clients6.google.fi, *.clients6.google.fr, *.clients6.google.ie, *.clients6.google.is, *.clients6.google.it, *.clients6.google.jp, *.clients6.google.nl, *.clients6.google.no, *.clients6.google.pl, *.clients6.google.pt, *.clients6.google.ro, *.clients6.google.ru, *.clients6.google.se, *.cloudendpointsapis.com, cloudendpointsapis.com, googleapis.com]
对此有何建议?我很确定云控制台中的一切都很好:api 密钥、限制、计费信息等。
解决方案
您可以尝试将自定义添加HostNameVerfier
到您的OkHttpClieknt
okHttpClient.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
如果你使用Retrofit
,设置OkHttpClient
如下:
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
Retrofit retrofit = new Retrofit.Builder() .client(okHttpClient).build();
推荐阅读
- python - 不断收到以下语法错误:async def on_ready():
- python - Streamlit 自动填充多选小部件以过滤数据框
- vba - Access DoCMD.OpenReport acViewPreview 从不打开预览并冻结 Access
- python - 从 CSV 文件 Python 创建列表
- typescript - 如何获取自定义TypeMap函数返回的默认类型
- javascript - 如何在数组中有一个可以根据编辑进行编辑的数字(删除或添加)
- java - Java,比较 ArrayList 的值
- django - 登录页面出错 - TypeError: __init__() got an unexpected keyword argument 'request'
- jquery - Vue中如何重复移动一个元素?
- reactjs - 列出文件以使用列表生成动态组件