gpt4 book ai didi

java - API 的问题输出为空。仅来自 intellij

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:43 24 4
gpt4 key购买 nike

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

我猜原因可能是在输出代码中的“Array”手镯“[]”中。

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

请帮帮我,我累了。

最佳答案

我修改了你的程序,检查一下这段代码。现在正在工作。您错过了 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;
}

关于java - API 的问题输出为空。仅来自 intellij,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56202962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com