向指定 URL发送GET方法的请求
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
方法中传入参数调用URL
public static String getTempInfo(参数 参数) {
JSONObject jsonObject = JSON.parseObject(TempInfoTest.sendGet("https://*******.com", "name1=" +参数1 + "&beginTime=2025-1-7&endTime=2022-2-17"+"&name2"+参数2));
net.sf.json.JSONObject jsonObject = JSONObject.fromObject(TempInfoTest.sendGet("https://××××.com","name1=" +参数1 + "&beginTime=2025-1-7&endTime=2022-2-17"+"&name2"+参数2));
return STRING;
通过上面这步我们如果想要拿到data里面的数据
String data = jsonObject.getString("data");
JSONArray jsonArray = JSON.parseArray(data);
如果要拿到data里面的大小
jsonArray.size()
如果要拿到data里面固定字符的值
String ×× = jsonArray.getJSONObject(i).getString("字符××");