【问题标题】:Sometimes the Google Geocoding API returns a 500 server error [closed]有时 Google 地理编码 API 返回 500 服务器错误 [关闭]
【发布时间】:2018-03-09 09:02:04
【问题描述】:

有时 Google Maps API 根据德国邮政编码返回 500 server error 响应,我不明白为什么。

我希望它足够具体。 有什么想法吗?

https://maps.googleapis.com/maps/api/geocode/json?key={api_key}&address={postal_code}&language=de&region=de&components=country:DE&sensor=false

【问题讨论】:

  • 你能分享任何代码吗?相同的邮政编码是否总是得到 500?
  • 代码如下。它并非总是发生!
  • https://maps.googleapis.com/maps/api/geocode/json?key={api_key}&address={postal_code}&language=de&region=de&components=country:DE&sensor=false 是查询参数。我通过 xhr (vue-resource) 向类发出获取请求
  • 但是,如果您为给定地址获得 500 分并重新提交相同的地址,您会再次获得 500 分吗?
  • 不,不适用于给定地址。有时有效,有时无效。如果我重新提交,它的工作原理...

标签: api google-maps server-error


【解决方案1】:

由于您指定问题不是给定地址,而是看似“随机”的行为,这可能属于其他“著名”API 的 documented 行为。

至于其他情况,推荐的策略是Exponential backoff for the Geocoding API,基本意思是一定延迟后重试。

如果上述链接失效或发生变化,我引用这篇文章:

指数退避

在极少数情况下,在满足您的请求时可能会出现问题;您可能会收到 4XX 或 5XX HTTP 响应代码,或者 TCP 连接可能只是在您的客户端和 Google 服务器之间的某处失败。通常值得重新尝试请求,因为当原始请求失败时,后续请求可能会成功。但是,重要的是不要简单地循环重复向 Google 的服务器发出请求。这种循环行为可能会使您的客户端和 Google 之间的网络过载,从而导致多方出现问题。

更好的方法是重试,增加尝试之间的延迟。通常,每次尝试都会使延迟增加一个乘法因子,这种方法称为指数退避。

例如,考虑一个希望向 Google Maps Time Zone API 发出此请求的应用程序:

https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&timestamp=1331161200&key=YOUR_API_KEY

以下 Python 示例展示了如何使用指数退避来发出请求:

import json
import time
import urllib
import urllib2

def timezone(lat, lng, timestamp):
    # The maps_key defined below isn't a valid Google Maps API key.
    # You need to get your own API key.
    # See https://developers.google.com/maps/documentation/timezone/get-api-key
    maps_key = 'YOUR_KEY_HERE'
    timezone_base_url = 'https://maps.googleapis.com/maps/api/timezone/json'

    # This joins the parts of the URL together into one string.
    url = timezone_base_url + '?' + urllib.urlencode({
        'location': "%s,%s" % (lat, lng),
        'timestamp': timestamp,
        'key': maps_key,
    })

    current_delay = 0.1  # Set the initial retry delay to 100ms.
    max_delay = 3600  # Set the maximum retry delay to 1 hour.

    while True:
        try:
            # Get the API response.
            response = str(urllib2.urlopen(url).read())
        except IOError:
            pass  # Fall through to the retry loop.
        else:
            # If we didn't get an IOError then parse the result.
            result = json.loads(response.replace('\\n', ''))
            if result['status'] == 'OK':
                return result['timeZoneId']
            elif result['status'] != 'UNKNOWN_ERROR':
                # Many API errors cannot be fixed by a retry, e.g. INVALID_REQUEST or
                # ZERO_RESULTS. There is no point retrying these requests.
                raise Exception(result['error_message'])

        if current_delay > max_delay:
            raise Exception('Too many retry attempts.')
        print 'Waiting', current_delay, 'seconds before retrying.'
        time.sleep(current_delay)
        current_delay *= 2  # Increase the delay each time we retry.

tz = timezone(39.6034810, -119.6822510, 1331161200)
print 'Timezone:', tz

当然这不会解决您提到的“虚假回复”;我怀疑这取决于数据质量并且不会随机发生。

【讨论】:

  • 好吧,它似乎工作。此外,我不明白为什么在不存在的邮政编码而不是“ZERO_RESULTS”中返回“Ludwigshafen am Rhein”?
  • 谁知道呢,这就是我所说的data quality。如果适合您,也许您可​​能想为这个问题选择我的答案?
猜你喜欢
  • 1970-01-01
  • 2011-07-20
  • 2022-08-03
  • 1970-01-01
  • 2012-07-25
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多