【问题标题】:Send request to Google Elevation API in Java用 Java 向 Google Elevation API 发送请求
【发布时间】:2014-05-12 11:38:59
【问题描述】:

我正在尝试向 Google 海拔 API 发送 Post 请求并期待响应

private final String ELEVATION_API_URL =  "https://maps.googleapis.com/maps/api/elevation/json";

private final String USER_AGENT = "Mozilla/5.0";

String urlParameters = "locations=6.9366681,79.9393521&sensor=true&key=<API KEY>";


URL obj = new URL(ELEVATION_API_URL);
            java.net.HttpURLConnection con = (java.net.HttpURLConnection)obj.openConnection();

            //add reuqest header
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setRequestProperty("Content-Language", "en-US");  

            String urlParameters = request;

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();

我正在以这种方式发送请求,但我收到的响应代码为 400。这在从浏览器发送请求时有效。这段代码有什么问题。

【问题讨论】:

    标签: java google-maps-api-3 google-api


    【解决方案1】:

    为了让我取回 XML,我对您的项目进行了以下更改

    StringBuilder response = new StringBuilder(); // placed on the top of your Class
    
    **wr.writeBytes(urlParameters.toString());** // as you have it in your code
    System.out.println("ResponseMessage : " + connection.getResponseMessage());
    System.out.println("RequestMethod : " + connection.getRequestMethod());
    
    in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    
    String inputLine;
    
    while ((inputLine = in.readLine()) != null) {
    
        response.append(inputLine);
    
    }
    
    in.close();
    
    wr.flush();
    
    wr.close();
    

    // 我将 URL 更改为 :
    私有最终字符串 ELEVATION_API_URL = "https://maps.googleapis.com/maps/api/elevation/xml";

    //**I get XML in the response** 
    
    return response.toString();
    

    【讨论】:

      【解决方案2】:

      我认为url参数有问题。

      首先是因为发送一个空的海拔 api 请求确实会返回一个代码 400 (Invalid request. Missing the 'path' or 'locations' parameter.)。

      其次,因为这有效(返回 200):

      public void test() throws Exception {
          String ELEVATION_API_URL =  "https://maps.googleapis.com/maps/api/elevation/json";
      
          String USER_AGENT = "Mozilla/5.0";
      
          String urlParameters = "locations=6.9366681,79.9393521&sensor=true";
      
      
          URL obj = new URL(ELEVATION_API_URL + "?" + urlParameters);
          HttpURLConnection con = (HttpURLConnection) obj.openConnection();
      
          //add reuqest header
          con.setRequestMethod("POST");
          con.setRequestProperty("User-Agent", USER_AGENT);
          con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
          con.setRequestProperty("Content-Language", "en-US");
      
          //String urlParameters = request;
      
          // Send post request
          con.setDoOutput(true);
          DataOutputStream wr = new DataOutputStream(con.getOutputStream());
          wr.writeBytes(urlParameters);
          wr.flush();
          wr.close();
      
          int responseCode = con.getResponseCode();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-16
        • 2015-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多