title: Google Directions API accessed via Java
body: <p>I'm trying to create a simple java application. It want to be able to input the origin and destination and to output the path it takes in xml. </p>
  
  <p>This is what i've got so far. My main focus is to get a xml output to the console. </p>
  
  <pre><code>import java.net.HttpURLConnection;
  import java.net.URL;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  
  import org.w3c.dom.Document;
  
  public class directionService{
  
  
      public static void main(String[] args) {
  
          String start = "geelong";
          String finish = "melbourne";
  
          String region = "au";
  
          System.out.println(calculateRoute(start,finish,region));
      }
  
      private static String calculateRoute(String start, String finish, String region){
          String result = "";
          String urlString = "http://maps.googleapis.com/maps/api/directions/json?sensor=false&amp;origin="+start+"&amp;destination="+finish;
          System.out.println(urlString);
  
          try{
              URL urlGoogleDirService = new URL(urlString);
  
              HttpURLConnection urlGoogleDirCon = (HttpURLConnection)urlGoogleDirService.openConnection();
  
              urlGoogleDirCon.setAllowUserInteraction( false );
              urlGoogleDirCon.setDoInput( true );
              urlGoogleDirCon.setDoOutput( false );
              urlGoogleDirCon.setUseCaches( true );
              urlGoogleDirCon.setRequestMethod("GET");
              urlGoogleDirCon.connect();
  
              DocumentBuilderFactory factoryDir = DocumentBuilderFactory.newInstance();
              DocumentBuilder parserDirInfo = factoryDir.newDocumentBuilder();
              Document docDir = parserDirInfo.parse(urlGoogleDirCon.getInputStream());
  
              urlGoogleDirCon.disconnect();
              result = docDir.toString();
  
  
          return result; 
          }
  
          catch(Exception e)
          {
              System.out.println(e);
              return null;
          }
  
      };
  
  }
  </code></pre>
  
  <p>It throws this error</p>
  
  <blockquote>
    <p>java.net.ConnectException: Connection timed out: connect</p>
  </blockquote>
  
  <p>I've tried getting data from other webservices and it returns an xml dump fine. I read about having a key in the google documentation, is this required? It seems to work fine if I directly input the query into my browser. </p>
  
  <blockquote>
    <p><a href="http://maps.googleapis.com/maps/api/directions/xml?sensor=false&amp;origin=geelong&amp;destination=melbourne" rel="nofollow">http://maps.googleapis.com/maps/api/directions/xml?sensor=false&amp;origin=geelong&amp;destination=melbourne</a></p>
  </blockquote>
  
  <p>Any help would be greatly appriciated!</p>
  
  <p>Thanks,
  Steve</p>