使用新地点自动完成预测 api

Autocomplete predictions with new Places api

我正在尝试更新我的自动完成适配器,以便将新的 Google 地点 api 用于 Android。使用已弃用的 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();  

该异常是由代码其他部分中使用的 HostnameVerifier 实施不当引起的。自定义 HostnameVerifier 默认设置为:

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;
                }
            }

        });

因此,如您所见,此代码 returns 为 googleapis.com 的错误,实际上是 Android 的新 Google Places 库使用的域.

这是我必须更新的项目中的遗留代码。 我真的不建议覆盖默认的 HostnameVerifier。