I'm trying to update my autocomplete adapter in order to use the new Google Places api for Android. Everything worked fine with the deprecated api but, after updating to the new one, I'm getting an odd exception. Here is my code:
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;
}
When the code reaches the Tasks.await line, throws an Exception like this:
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]
Any advice on this? I'm pretty sure that everything is ok in the cloud console: api key, restrictions, billing information, etc.
The Exception was provoked by a poor HostnameVerifier
implementation used in other part of the code. The custom HostnameVerifier
was setted by default this way:
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
if(arg0.contains("mydomain.com") || arg0.contains("myotherdomain.com")) {
return true;
}else {
return false;
}
}
});
So, as you can see, this code returns false with googleapis.com, which in fact is the domain used by the new Google Places library for Android.
This is legacy code in a project I must update. I really do not recommend overriding the default HostnameVerifier.