Google Places API在第二次请求后未返回

时间:2015-09-07 04:37:51

标签: java google-places-api google-places

我有几个坐标对,我正在尝试在Google商家信息中查询信息。问题是在第二个请求之后(每个请求与另一个请求相距至少1秒),第三个请求永远不会返回 - 它只是无限期挂起。

以下是我用来调用Google商家信息的代码;

Map<Double, Double> coordinates = new HashMap<>();
coordinates.put(40.7549309, -73.9840195);
coordinates.put(48.8616147, 2.3355015);
coordinates.put(36.106965, -112.112997);


for(Map.Entry<String, String> entry : coodinates.entrySet()){
    getNearbyPlaces(entry.getKey(), entry.getValue(), 50);
}

负责代码在这里;

private static final String API_URL = "https://maps.googleapis.com/maps/api/place/json/key=%s&location=%f,%f&radius=%f";

private final HttpClient client = HttpClientBuilder.create().build();

public List<GeoLocation> getNearbyPlaces(final Double lat, final Double lng, final Double radius) {
    try {
        final String uri = String.format(API_URL, getApiKey(), lat, lng, radius);
        return getPlaces(uri, limit);
    } catch (Exception e) {
        LOGGER.error(String.format("error getting nearby places by lat=%f lng=%f", lat, lng), e);
        throw new RuntimeException(e);
    }
}

private List<Place> getPlaces(final String uri, final Integer limit) throws IOException {

    Integer maxLimit = Math.min(limit, MAXIMUM_RESULTS); // max of 60 results possible
    int pages = (int) Math.ceil(maxLimit / (double) MAXIMUM_PAGE_RESULTS);

    final List<Place> places = new LinkedList<>();

    String updatedUri = uri;
    // new request for each page
    for (int i = 0; i < pages; i++) {
        final String raw = this.get(updatedUri);
        places.add(new Place(parseRawIntoPlace(raw)));  
        final String nextPage = parseNextPage(raw);
        if (nextPage != null) {
            maxLimit -= MAXIMUM_PAGE_RESULTS;
            updatedUri = getNextUri(nextPage);
            sleep(3000); // Page tokens have a delay before they are available... 3 seconds ought to be plenty ...
        } else {
            break;
        }
    }

    return places;
}


private String get(final String uri) throws IOException {
    try {
        final HttpGet get = new HttpGet(uri);
        return readString(client.execute(get));
    } catch (Exception e) {
        throw new IOException(e);
    }
}

值得注意的是,这段代码在大约几个星期前一直运行良好。这是最近的一种表现形式。

如果我杀死了应用程序并再次运行它,它将完成两个请求,然后再次挂在第三个请求上。

我尝试了不同顺序的不同坐标 - 我仍然得到相同的结果(我试图消除谷歌不喜欢特定坐标对的极端可能性)

非常感谢任何帮助。

仅供参考 - 我对Places API的使用完全符合其使用政策,也是每天2500个配额的一小部分。

0 个答案:

没有答案