2012-04-02 5 views
0

Ich verwende Google Maps API. Wenn ich die folgende URL nehme und sie im Browser eingebe, bekomme ich ein Popup-Fenster, um eine JSON-Zeichenfolge als Notepad-Datei zu speichern.Lesen einer JSON-Zeichenfolge aus Google Maps-API

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

... aber wenn ich das folgende Stück Code schreiben, das gleiche zu programmatisch tun, gibt es die folgende Ausnahme:

Ausnahme:

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(); }

Weiß jemand, welchen Fehler ich hier mache?

Dank Abhishek S

Antwort

2

Sie müssen Leerzeichen löschen:

Verwendung:

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

Statt:

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

Dank Ernesto. Das war perfekt! Wissen Sie, ob solche Aufrufe an Google API unbegrenzt sind? Ich muss unbegrenzte Anrufe wie diese machen. –

+1

Überprüfen Sie dies: https://developers.google.com/maps/documentation/directions/#Limits –