Depois de eu enviar o request ele devolve uma resposta JSON. O que eu queria saber é como recebo essa resposta no meu código e trabalho nela. Estou a trabalhar com java + seam.
public static final void main (String[] argv) throws IOException, XPathExpressionException, ParserConfigurationException, SAXException {
// query address
String address = "1600 Amphitheatre Parkway, Mountain View, CA";
// prepare a URL to the geocoder
URL url = new URL(GEOCODER_REQUEST_PREFIX_FOR_XML + "?address=" + URLEncoder.encode(address, "UTF-8") + "&sensor=false");
// prepare an HTTP connection to the geocoder
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Document geocoderResultDocument = null;
try {
// open the connection and get results as InputSource.
conn.connect();
InputSource geocoderResultInputSource = new InputSource(conn.getInputStream());
// read result and parse into XML Document
geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(geocoderResultInputSource);
} finally {
conn.disconnect();
}
// prepare XPath
XPath xpath = XPathFactory.newInstance().newXPath();
// extract the result
NodeList resultNodeList = null;
// a) obtain the formatted_address field for every result
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result/formatted_address", geocoderResultDocument, XPathConstants.NODESET);
for(int i=0; i<resultNodeList.getLength(); ++i) {
System.out.println(resultNodeList.item(i).getTextContent());
}
// b) extract the locality for the first result
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/address_component[type/text()='locality']/long_name", geocoderResultDocument, XPathConstants.NODESET);
for(int i=0; i<resultNodeList.getLength(); ++i) {
System.out.println(resultNodeList.item(i).getTextContent());
}
// c) extract the coordinates of the first result
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/geometry/location/*", geocoderResultDocument, XPathConstants.NODESET);
float lat = Float.NaN;
float lng = Float.NaN;
for(int i=0; i<resultNodeList.getLength(); ++i) {
Node node = resultNodeList.item(i);
if("lat".equals(node.getNodeName())) lat = Float.parseFloat(node.getTextContent());
if("lng".equals(node.getNodeName())) lng = Float.parseFloat(node.getTextContent());
}
System.out.println("lat/lng=" + lat + "," + lng);
// c) extract the coordinates of the first result
resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/address_component[type/text() = 'administrative_area_level_1']/country[short_name/text() = 'US']/*", geocoderResultDocument, XPathConstants.NODESET);
float lat = Float.NaN;
float lng = Float.NaN;
for(int i=0; i<resultNodeList.getLength(); ++i) {
Node node = resultNodeList.item(i);
if("lat".equals(node.getNodeName())) lat = Float.parseFloat(node.getTextContent());
if("lng".equals(node.getNodeName())) lng = Float.parseFloat(node.getTextContent());
}
System.out.println("lat/lng=" + lat + "," + lng);
}
}[/code]
Erro:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at GeocodingSample.main(GeocodingSample.java:37)