java使用在线api实例

字符串 strUrl为访问地址和参数

public String loadAddrsApi() {
    StringBuffer sb;
    String strUrl = "https://api.apishop.net/common/postcode/getAddrs?apiKey=个人的apiKey";
    try {
        URL url = new URL(strUrl);
        URLConnection con = url.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String line;
        sb = new StringBuffer();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
        sb = new StringBuffer();
    }
    return sb.toString();
}

 

posted @ 2019-11-15 15:03  想看云飞却没风~  阅读(1512)  评论(0编辑  收藏  举报