Time out em consulta google maps com SAXReader

0 respostas
S

Estou tentando utilizar essa classe para calcular distancia entre dois endereços, porém com frequência recebo time out. Se imediatamente insiro a url no navegador o retorno não falha.
Imagino que o problema seja o JAXReader. Conhecem outra forma de acessar o webserver da Google com Java?
Este codigo foi publicado em 2012 por Gabriel em https://wehavescience.wordpress.com/2012/12/14/distancia-entre-dois-enderecos-usando-java-e-google-directions-api/

import java.net.MalformedURLException;

import java.net.URL;

import java.util.List;
import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;
public class DistanciaWS {

public static String calcular(String origem, String destino) {

URL url;

try {

url = new URL(

<a href="http://maps.google.es/maps/api/directions/xml?origin=">http://maps.google.es/maps/api/directions/xml?origin=</a>”

+ origem + &destination= + destino

+ &sensor=false);
Document document = getDocumento(url);

        return analisaXml(document);
    } catch (MalformedURLException | DocumentException e) {
        e.printStackTrace();
    }
    return "";
}

@SuppressWarnings("rawtypes")
public static String analisaXml(Document document) {
    List list = document
            .selectNodes("//DirectionsResponse/route/leg/distance/text");

    Element element = (Element) list.get(list.size() - 1);

    return element.getText();
}

public static Document getDocumento(URL url) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(url);
    return document;
}

public static void main(String[] args) {
    System.out.println(calcular("Rua Augusta, 500, Sao Paulo - SP",
            "Avenida Liberdade, 800, Sao Paulo - SP"));
}

}

Criado 4 de dezembro de 2019
Respostas 0
Participantes 1