参考:https://www.cnblogs.com/swordfall/p/10757499.html
在代码中,可能需要一个工具类,能够调用第三方API 获取一些数据
/** * 以 Get 方法调用API */ public static void doGet(String pathUrl,String data) throws IOException{ OutputStreamWriter out = null; BufferedReader br = null; String result = ""; try { URL url = new URL(pathUrl); //打开和 URL 之间的连接 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); //设置通用属性 conn.setRequestProperty("accept", "*/*"); InputStream is = (InputStream) conn.getInputStream(); //构造一个字符缓冲流 br = new BufferedReader(new InputStreamReader(is)); String str = ""; while((str=br.readLine())!=null) { result +=str; } System.out.println(result); //关闭流 is.close(); conn.disconnect(); }catch(Exception e) { e.printStackTrace(); }finally { try { if(out != null) { out.close(); } if(br != null) { br.close(); } }catch(IOException e) { e.printStackTrace(); } } }
测试类:
public class Main { public static void main(String[] args) throws Exception{ MyHttpUrlConnection.doGet("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071", ""); } }