0

I am using the Google Maps api. When I take the following URL and enter it in the browser, I get a pop up window to save a JSON String as a notepad file.

URL: https://maps.googleapis.com/maps/api/directions/json?origin=Bangalore, India&destination=Belgaum, India&sensor=false

... but when I write the following piece of code to programmatically do the same, there is the following exception:

Exception:

java.io.IOException: Server returned HTTP response code: 400 for URL: https://maps.googleapis.com/maps/api/directions/json?origin=Bangalore, India&destination=Belgaum, India&sensor=false
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)

Code:

try {
        url = new URL("https://maps.googleapis.com/maps/api/directions/json?origin=Bangalore, India&destination=Belgaum, India&sensor=false");
        try {
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.connect();
            //InputStream is = conn.getInputStream();
            InputSource geocoderResultInputSource = new    
            InputSource(conn.getInputStream());

              // read result and parse into XML Document
              try {
                geocoderResultDocument = 

DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(geocoderResultInputSource); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); }

Does anyone know what mistake am I doing here?

Thanks Abhishek S

1 Answer 1

2

You need to delete blank spaces:

Use:

new URL("https://maps.googleapis.com/maps/api/directions/json?origin=Bangalore,India&destination=Belgaum,India&sensor=false");

Instead of:

new URL("https://maps.googleapis.com/maps/api/directions/json?origin=Bangalore, India&destination=Belgaum, India&sensor=false");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ernesto. That was perfect! Do you know if such calls to Google API is unlimited? I need to make unlimited calls like these.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.