简体   繁体   English

Android:Google API v.3提供INVALID_REQUEST,但在浏览器中执行良好

[英]Android : Google API v.3 gives INVALID_REQUEST , but executed good in the browser

I need to geocode the address and get latitude , longitude from the city, postal code and other. 我需要对地址进行地理编码,并从城市,邮政编码等获取纬度,经度。

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

I receive INVALID_REQUEST as a response. 我收到INVALID_REQUEST作为答复。 But when I try this link in browser or with the help of REST client, everything works fine. 但是,当我在浏览器中或在REST客户端的帮助下尝试此链接时,一切正常。

The same with this request : 与此请求相同:

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

What can be the reason ? 可能是什么原因?

Url creation : 网址创建:

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

Here is an idea: Check the output of your logcat. 这是一个主意:检查logcat的输出。

If the request time and the time when the data was sent back from google are too close to each other, Google might assume you are a spammer and deny access to the server on the second request. 如果请求时间和从Google发回数据的时间彼此距离太近,则Google可能会认为您是垃圾邮件发送者,并拒绝在第二次请求时访问服务器。

The solution in this case is wait 1 second if there is a next_page_token and then send the followup request. 在这种情况下,解决方案是如果存在next_page_token,则等待1秒钟,然后发送后续请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Places API,添加位置始终返回INVALID_REQUEST - Google Places API, Adding a place always returns INVALID_REQUEST 谷歌日历 API Java 400 invalid_request - Google Calendar API Java 400 invalid_request Linkedin Android SDK - 无法连接到API(INVALID_REQUEST) - Linkedin Android SDK - Unable to connect to API (INVALID_REQUEST) 添加位置后,Google Places POST“ INVALID_REQUEST”响应 - Google Places POST “INVALID_REQUEST” response when adding a place 用于获取访问令牌的Google oauth java客户端失败,并显示“400 Bad Request {”error“:”invalid_request“}” - Google oauth java client to get an access token fails with “400 Bad Request { ”error“ : ”invalid_request“ }” 用于获取访问令牌的Google oauth java客户端失败,并显示“400 Bad Request {”error“:”invalid_request“}” - Google oauth java client to get an access token fails with “400 Bad Request { ”error“ : ”invalid_request“ }” 使用搜索API将具有重复字段名称的文档添加到索引时出现INVALID_REQUEST异常 - INVALID_REQUEST exception when adding a document with repeated field names to an index with the search API 为什么Google OAuth 2.0代码令牌交换会返回“ invalid_request”? - Why does Google OAuth 2.0 code-token Exchange returns “invalid_request”? Android上的Google翻译API给出错误“ GoogleJsonResponseException:400错误的请求” - Google translate api on android gives error 'GoogleJsonResponseException: 400 Bad Request' 使用Google maps api v2执行时应用程序崩溃 - Application crashes when is executed using Google maps api v2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM