有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

API的java问题输出为空。只有来自intelliji

我的程序输出为空。我不知道为什么。当我在Firefox中输入请求时,一切都很好

我猜原因可能是输出代码中的“数组”手镯“[]”

public class Connect {

    public String connect() {
        String  output = null;

        try {
            URL url = new URL("http://aviation-edge.com/v2/public/flights?key=[key]=3");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP Error code : "
                        + conn.getResponseCode());
            }
            InputStreamReader in = new InputStreamReader(conn.getInputStream());
            BufferedReader br = new BufferedReader(in);
            output = br.readLine();
            System.out.println(output);
            br.close();

        } catch (Exception e) {
            System.out.println("Exception in WeatherApi:- " + e);
        }
        return output;
    }

}

Firefox的输出--->

[ { "geography": { "latitude": 42.3101, "longitude": -85.5977, "altitude": 9700.26, "direction": 91.15 }, "speed": { "horizontal": 920.628, "isGround": 0, "vertical": 30.42 }, "departure": { "iataCode": "ORD", "icaoCode": "ORD" }, "arrival": { "iataCode": "YYZ", "icaoCode": "YYZ" }, "aircraft": { "regNumber": "N811UA", "icaoCode": "A319", "icao24": "AB0ED6", "iataCode": "A319" }, "airline": { "iataCode": "UA", "icaoCode": "UAL" }, "flight": { "iataNumber": "UA228", "icaoNumber": "UAL228", "number": "228" }, "system": { "updated": "1558214460", "squawk": "4373" }, "status": "en-route" } ]

请帮帮我,我累了


共 (1) 个答案

  1. # 1 楼答案

    我已经修改了你的程序,请检查此代码。它现在正在工作。你错过了while条件

    public String connect() {
        String output = null;
        String key = "679820-995fb7";
    
        try {
          String connectUrlString =
              "http://aviation-edge.com/v2/public/flights?key=" + key + "&limit=3";
          System.out.println("URL String : " + connectUrlString);
          //      URL url = new URL("http://aviation-edge.com/v2/public/flights?key=[key]=3");
          URL url = new URL(connectUrlString);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("GET");
          conn.setRequestProperty("Accept", "application/json");
    
          if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP Error code : " + conn.getResponseCode());
          }
    
          BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
          String inputLine;
          StringBuffer response = new StringBuffer();
    
          while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
          }
          in.close();
          output = response.toString();
        } catch (Exception e) {
          System.out.println("Exception in WeatherApi:- " + e);
        }
        return output;
      }