2012-04-02 73 views
0

我正在使用Google Maps API。当我使用以下URL并在浏览器中输入时,我会弹出一个窗口,将JSON字符串保存为记事本文件。从Google Maps API读取JSON字符串

网址:https://maps.googleapis.com/maps/api/directions/json?origin=Bangalore,印度&目的地=贝尔高姆,印度&传感器=假

...但是当我写了下面这段代码以编程方式做同样的,有以下异常:

例外:

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) 

代码:

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(); (ParserConfigurationException e){ e.printStackTrace(); }

有谁知道我在这里做什么错误?

感谢 阿布舍克小号

回答

2

您需要删除空格:

用途:

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

相反的:

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

谢谢埃内斯托。那太完美了!你知道这样的Google API调用是无限的吗?我需要做这样的无限通话。 – 2012-04-02 04:11:46

+1

选中此项:https://developers.google.com/maps/documentation/directions/#Limits – 2012-04-02 04:29:41