javagoogle-directions-apidom4j

Java Google Directions API


I am developing a Java Web application tha reads an Excel with adresses. (students adresses)

I need to get the distance from one address to each one different adress in the spreadsheet (how far from the school adress each students live)

I'm using Dom4J library and the tests with two inputs (origin and destination) to retreive one distance is ok, but when I read the excel file and try to retreive each distance I get:

Nested exception: java.io.IOException: Server returned HTTP response code: 400 for URL: https://maps.google.es/maps/api/directions/xml?origin=RUA%20JOÃO%20PESSOA,%201316,%20C/03,%20Nilopolis&destination=Rua%20João%20Pessoa,Centro&key=mykey at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1997) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1589) at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224) at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:677) at java.xml/com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:150) at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:861) at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:825) at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1224) at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:637) at org.dom4j.io.SAXReader.read(SAXReader.java:465) at org.dom4j.io.SAXReader.read(SAXReader.java:291) at util.BibliotecaGoogle$DistanciaWS.getDocumento(BibliotecaGoogle.java:45) at util.BibliotecaGoogle$DistanciaWS.calcular(BibliotecaGoogle.java:24) at manager.Edu_turmas_impBean.determinardistancia(Edu_turmas_impBean.java:120) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.apache.el.parser.AstValue.invoke(AstValue.java:246) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:266) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) at org.primefaces.application.DialogActionListener.processAction(DialogActionListener.java:54) at javax.faces.component.UICommand.broadcast(UICommand.java:315) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1726) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:833)

Here is my code:

         public static String calcular(String origem, String destino) {
            URL url;
            try {
                String urlformatada = "https://maps.google.es/maps/api/directions/xml?origin="
                        + origem + "&destination=" + destino
                        + "&key=mykey"; 
                
                url = new URL(urlformatada.replace(" ", "%20"));
     
                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;
        }

Solution

  • Thank you for the reply. The problem was that when you fill the command on the browser it makes the proper treatments, but when you encapsulate in your code you need to treat all. For example: these characters "ç", ":", "ã" in the origin or destination.