Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Google放置API在第二次请求后不返回_Java_Google Places Api_Google Places - Fatal编程技术网

Java Google放置API在第二次请求后不返回

Java Google放置API在第二次请求后不返回,java,google-places-api,google-places,Java,Google Places Api,Google Places,我有几个坐标对,我正试图在谷歌的位置上查询信息。问题是,在第二个请求之后(每个请求至少相隔1秒),第三个请求永远不会返回——它只是无限期地挂在那里 下面是我用来称呼Google Places的代码 Map<Double, Double> coordinates = new HashMap<>(); coordinates.put(40.7549309, -73.9840195); coordinates.put(48.8616147, 2.3355015); coordi

我有几个坐标对,我正试图在谷歌的位置上查询信息。问题是,在第二个请求之后(每个请求至少相隔1秒),第三个请求永远不会返回——它只是无限期地挂在那里

下面是我用来称呼Google Places的代码

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);
}
Map坐标=新的HashMap();
坐标.put(40.7549309,-73.9840195);
坐标.put(48.8616147,2.3355015);
坐标.put(36.106965,-112.112997);
for(Map.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);
    }
}
private静态最终字符串API\u URL=”https://maps.googleapis.com/maps/api/place/json/key=%s&location=%f,%f&半径=%f”;
私有最终HttpClient客户端=HttpClientBuilder.create().build();
公共列表getNearbyPlaces(最终双lat、最终双lng、最终双半径){
试一试{
最终字符串uri=String.format(API_URL,getApiKey(),lat,lng,radius);
返回getPlaces(uri,limit);
}捕获(例外e){
LOGGER.error(String.format(“通过lat=%f lng=%f获取附近位置的错误”,lat,lng),e);
抛出新的运行时异常(e);
}
}
私有列表getPlaces(最终字符串uri,最终整数限制)引发IOException{
整数maxLimit=Math.min(限制,最大结果);//最多可能有60个结果
int pages=(int)Math.ceil(maxLimit/(double)最大页面结果);
最终列表位置=新建LinkedList();
字符串updateURI=uri;
//每一页的新请求
对于(int i=0;i
值得注意的是,这段代码在几周前还运行得很好。这是最近的表现

如果我关闭应用程序并再次运行,它将完成两个请求,然后再次挂起第三个请求

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

非常感谢您的帮助


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

也许您达到了HttpClient中默认的maxConnections限制2。检查您的
readString
方法是否正在CloseableHttpResponse上调用
close
,或者作为一个快速测试,查看
client.setMaxConnPerRoute(3)
是否起作用。如果没有更完整的例子,很难判断这是否是问题所在。