【问题标题】:Reading a JSON String from Google Maps API从 Google Maps API 读取 JSON 字符串
【发布时间】:2012-04-15 18:30:16
【问题描述】:

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

网址:https://maps.googleapis.com/maps/api/directions/json?origin=Bangalore,印度&destination=Belgaum,印度&sensor=false

...但是当我编写以下代码以编程方式执行相同操作时,出现以下异常:

例外:

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

有人知道我在这里犯了什么错误吗?

谢谢 阿布舍克S

【问题讨论】:

    标签: java google-maps


    【解决方案1】:

    你需要删除空格:

    用途:

    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");
    

    【讨论】:

    • 谢谢埃内斯托。那太完美了!您知道对 Google API 的此类调用是否无限制?我需要像这样无限制地拨打电话。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多