写一个java通过get方法访问url获取json的代码
时间: 2023-03-15 19:19:21 浏览: 115
try {
URL url = new URL("http://www.example.com/json");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect(); JSONObject jsonObject = new JSONObject(content.toString());
// 在此处处理获取到的json数据} catch (Exception e) {
e.printStackTrace();
}
阅读全文