【问题标题】:Android : Google API v.3 gives INVALID_REQUEST , but executed good in the browserAndroid:Google API v.3 提供 INVALID_REQUEST,但在浏览器中执行良好
【发布时间】:2015-07-24 10:26:37
【问题描述】:

我需要对地址进行地理编码并获取城市的纬度、经度、邮政编码等。

当我将此请求与 Android 一起使用时 https://maps.googleapis.com/maps/api/geocode/json?address=Aachen-Horbach+Gzg&region=DEU&sensor=false

我收到 INVALID_REQUEST 作为响应。但是当我在浏览器中或借助 REST 客户端尝试此链接时,一切正常。

这个请求也一样:

https://maps.googleapis.com/maps/api/geocode/json?address=Abertamy-Horní+Blatná&region=CZE&sensor=false

可能是什么原因?

网址创建:

     StringBuilder addressUrl  = new StringBuilder();
                    if (order.getDepartureAddress().getStreet()!=null && !order.getDepartureAddress().getStreet().equalsIgnoreCase(""))
                        addressUrl.append(order.getDepartureAddress().getStreet() + ", ");
                    if (order.getDepartureAddress().getHouseNumber()!=null && ! order.getDepartureAddress().getHouseNumber().equalsIgnoreCase(""))
                        addressUrl.append(order.getDepartureAddress().getHouseNumber() + ", ");
                    if (order.getDepartureAddress().getCity()!=null && !order.getDepartureAddress().getCity().equalsIgnoreCase(""))
                        addressUrl.append(order.getDepartureAddress().getCity() );
                    if (order.getDepartureAddress().getCountryCode()!=null && !order.getDepartureAddress().getCountryCode().equalsIgnoreCase(""))
                        addressUrl.append("&region="+order.getDepartureAddress().getCountryCode() );
                    addressUrl.append("&sensor=false");
/* 
Constants.URL_GEOCODING = https://maps.googleapis.com/maps/api/geocode/json?address=    
*/
String finalUrl = Constants.URL_GEOCODING + addressUrl.toString();

/* request execution : */


    public static JSONObject readJsonFromUrl(String urlStr) throws IOException, JSONException {
            URL url = new URL(urlStr);
            URI uri = null;
            URI uri2= null;
            try {
                uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
                url = uri.toURL();
                uri2 = new URI(url.toString().replace("%20", "+"));
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            url = uri2.toURL();
            Log.i(TAG + " requested url", url.toString());
            InputStream is = url.openStream();
            try {
                BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
                String jsonText = readAll(rd);
                JSONObject json = new JSONObject(jsonText);
                return json;
            } finally {
                is.close();
            }
        }

【问题讨论】:

  • 我喜欢你的名字。 Rikki Tikki Tikki Tavi
  • 也许你有编码问题。请添加你称之为url的android代码
  • 将其添加到我的问题中

标签: java android google-maps google-maps-api-3


【解决方案1】:

这是一个想法:检查 logcat 的输出。

如果请求时间和从 Google 发回数据的时间太接近,Google 可能会认为您是垃圾邮件发送者,并在第二次请求时拒绝访问服务器。

这种情况的解决方案是,如果有next_page_token,则等待1秒,然后发送后续请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2018-09-12
    • 2019-03-16
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多