使用新的Places API的自动完成预测

时间:2019-06-06 14:59:56

标签: android google-places-api autocompletetextview googleplacesautocomplete

我正尝试更新我的自动完成适配器,以便使用适用于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密钥,限制,账单信息等。

2 个答案:

答案 0 :(得分:0)

您可以尝试将自定义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();  

答案 1 :(得分:0)

该异常是由代码其他部分中使用的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;
                }
            }

        });

因此,如您所见,此代码在googleapis.com(实际上是新的Android版Google Places库使用的域)中返回false。

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