package cn.wangshiyu777; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class JsonUtil { public static String loadJson(String url) throws Exception { //读取url,返回json串 StringBuilder json = new StringBuilder(); URL oracle = new URL(url); URLConnection yc = oracle.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); String inputLine = null; while((inputLine = in.readLine()) != null){ json.append(inputLine); } in.close(); return json.toString(); } public static void main(String[] args) throws Exception { String url = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"; String json = loadJson(url); System.out.println(json); } }
相关jar包
链接:https://pan.baidu.com/s/1oRqRLnbLVL83k5_aQwrmlw
提取码:uqqx