API 的问题输出为空.仅来自intelliji [英] Problem output from API is empty. Only from intelliji

查看:19
本文介绍了API 的问题输出为空.仅来自intelliji的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序有空输出.我不知道为什么.当我在 Firefox 中输入请求时,一切正常.

I have empty output from my program. I don't know why. When i enter in request in Firefox everything is fine.

我猜原因可能是在输出代码中的阵列"手镯[ ]"中.

I guess the reason could be in "Array" bracelets "[ ] " within output code.

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 的输出 --->

Output from 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" } ]

请帮助我累了.

推荐答案

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

I have modified your program, check this code. It is working now. You missed the while condition.

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

这篇关于API 的问题输出为空.仅来自intelliji的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆