我正在尝试将https://us.mc-api.net/ 中的 API用于项目,并将其作为测试。
public static void main(String[] args){
try {
URL url = new URL("http://us.mc-api.net/v3/uuid/193nonaxishsl/csv/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
System.out.println("I/O Error");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个 IOException 错误,但是当我在网络浏览器中打开同一个页面时,我得到
false,Unknown-Username
Run Code Online (Sandbox Code Playgroud)
这是我想从代码中得到的。我是新手,真的不知道为什么会发生这种情况或为什么会这样。编辑:堆栈跟踪
java.io.FileNotFoundException: http://us.mc-api.net/v3/uuid/193nonaxishsl/csv/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at com.theman1928.Test.Main.main(Main.java:13)
Run Code Online (Sandbox Code Playgroud)
URL 正在返回状态代码 404,因此输入流(这里是轻度猜测)没有被创建,因此为空。对状态代码进行排序,您应该没问题。
用这个 CSV 运行它,它很好:其他 csv
如果错误代码对您很重要,那么您可以使用 HttpURLConnection:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println("code:"+conn.getResponseCode());
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以在进行快速 if-then-else 检查之前处理响应代码。
| 归档时间: |
|
| 查看次数: |
5052 次 |
| 最近记录: |