首页 文章

Jersey客户端挂起MapQuest请求

提问于
浏览
0

我试图通过MapQuest REST API使用地理编码来获取一组纬度/经度坐标的地址 . 我正在服务器端使用Jersey客户端来提取这些数据 .

出于某种原因,当GET运行时,它永远不会返回并最终超时 . 起初我以为我试图访问不存在的资源或其他东西 . 但是,如果我获取我正在访问的资源的字符串值并将其粘贴到我的浏览器窗口中,我会为我指定的坐标返回有效的JSON . 我的日志中没有任何输出可以指出问题所在 .

这是我的代码:

private void getLocation(UserTemplate template) throws ConnectException
{     
   double lon = template.getLocation().getCoordinates()[0];
   double lat = template.getLocation().getCoordinates()[1];

   Client c = Client.create();
   WebResource resource = c.resource(Adapter.geocodeUrl + "?key=" + 
      Adapter.mapquestKey + "&location=" + lat + "," + lon);

   ClientResponse response = 
       resource.accept(MediaType.APPLICATION_JSON)
      .type(MediaType.APPLICATION_JSON)
      .get(ClientResponse.class);
}

为什么这个时机到了?

任何帮助,将不胜感激!

1 回答

  • 0

    我不知道为什么它超时,但我使用WebTarget而不是WebResource成功了:

    Client client = ClientBuilder.newClient();
    
    WebTarget target = client.target("http://open.mapquestapi.com/elevation/v1/profile" 
            + "?key=" + MAPQUEST_API_KEY + "&latLngCollection=" + "39.74012,-104.9849,39.7995,-105.7237,39.6404,-106.3736");
    
    Invocation.Builder invocationBuilder =
            target.request(MediaType.APPLICATION_JSON_TYPE);
    Response response = 
            invocationBuilder.get();
    System.out.println(response.getStatus());
    System.out.println(response.readEntity(String.class));
    

    您找到了解决问题的方法吗?

相关问题