import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import net.sf.json.JSONObject; public class TestIp { public static void main(String[] args) throws Exception { URL url = new URL("http://apistore.baidu.com/microservice/iplookup?ip=106.6.104.72"); URLConnection con = url.openConnection(); InputStream is = con.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuffer buffer = new StringBuffer(); String line =null; while(null != (line = br.readLine())) { buffer.append(line); } br.close(); isr.close(); is.close(); System.out.println(buffer.toString()); JSONObject jsonObject = JSONObject.fromObject(buffer.toString()); System.out.println(jsonObject); JSONObject jsonObject1 = JSONObject.fromObject(jsonObject.get("retData")); System.out.println(jsonObject1.get("province").toString()+jsonObject1.get("city").toString()+ "(" +jsonObject1.get("carrier").toString() + ")"); } }
时间: 2024-10-05 05:33:23