Java API的问题输出为空。只有来自intelliji
我的程序输出为空。我不知道为什么。当我在Firefox中输入请求时,一切都很好 我猜原因可能是输出代码中的“数组”手镯“[]”Java API的问题输出为空。只有来自intelliji,java,api,Java,Api,我的程序输出为空。我不知道为什么。当我在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"
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的输出--->
请帮助我,我累了。我已经修改了你的程序,请检查此代码。它现在正在工作。你错过了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;
}
我已经修改了你的程序,请检查此代码。它现在正在工作。你错过了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;
}
当我在google chrome中调用这个url时,我得到{“error”:{“text”:“Invalid API KEY”}。您是否已将密钥保存在firefox中?您好,没有http是正确的,我已隐藏密钥:)firefox的输出只是一个示例。我只是想说,java可能认为“[]”之前是数组。我不知道如何退出。您必须以编程方式在请求中传递隐藏密钥。我知道!:)我只在堆栈溢出上隐藏了密钥。这是完整的请求。现在我的输出是“[”。当我在google chrome中调用这个url时,我得到了{“错误”:{“文本”:“无效的API密钥”}。你在firefox中保存了密钥了吗?嗨,没有http是正确的,我有隐藏的密钥:)firefox的输出只是一个例子。我只是想说java可能认为“[]”之前是数组。我不知道如何退出它。你必须以编程方式在请求中传递隐藏键。我知道!:)我这里只有堆栈溢出的隐藏键。这是完整的请求。现在我的输出是“[”。。