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
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");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.