import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.io.BufferedReader;
import java.io.InputStreamReader;c
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
public class GetRequestExample {
public static void main(String[] args) throws Exception {
String url = "https://www.baidu?id=878";
// 创建URL对象
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为GET
con.setRequestMethod("GET");
// 可选: 添加请求头
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
// 读取响应
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 假设响应是一个JSON数组
List<Map<String, Object>> result = JSON.parseObject(response.toString(),
new TypeReference<List<Map<String, Object>>>(){});
// 打印解析后的结果
for (Map<String, Object> item : result) {
String id= item.get("id").toString();
System.out.println(id);
System.out.println(item);
}
// 打印结果
//System.out.println(response.toString());
}
}
java get 请求返回 list集合接受
于 2024-09-06 11:26:14 首次发布