URL url = new URL("http://10.58.169.114:21011/xw/satellite/all");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONObject jsonObject = JSONObject.parseObject(response.toString());
System.out.println(jsonObject.toString());
} else {
System.out.println("API调用失败,状态码为 " + responseCode);
}
JAVA获取API接口的数据并写入JSON对象中
最新推荐文章于 2024-01-17 15:13:55 发布
该代码示例展示了如何使用Java的HttpURLConnection类发送GET请求到指定URL(http://10.58.169.114:21011/xw/satellite/all),获取响应码并解析返回的JSON数据。如果响应成功,则打印JSON对象;否则,输出错误信息和状态码。
摘要由CSDN通过智能技术生成