前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。
import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class NewMobile { public static void main(String[] args) { System.out.println(NewMobile.getCarrier("")); System.out.println(NewMobile.getCity("")); } //得到归属地 public static String getCity(String tel) { //获取返回结果 String json = httpRequest(tel).toString(); //拆分xml页面代码 String[] a = json.split("city"); //得到归属地 String city = a[1].replace(">", "").replace("</", ""); return city; } //得到运营商 public static String getCarrier(String tel) { //获取返回结果 String json = httpRequest(tel).toString(); //拆分xml页面代码 String[] a = json.split("city"); String[] b = a[2].split("supplier"); //得到运营商 String carrier = b[1].replace(">", "").replace("</", ""); return carrier; } / * 发起http请求获取返回结果 * @param tel 待查询手机号 * @return String 结果字符串 */ public static String httpRequest(String tel) { //组装查询地址(requestUrl 请求地址) String requestUrl = "http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile="+tel; StringBuffer buffer = new StringBuffer(); try { URL url = new URL(requestUrl); HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection(); httpUrlConn.setDoOutput(false); httpUrlConn.setDoInput(true); httpUrlConn.setUseCaches(false); httpUrlConn.setRequestMethod("GET"); httpUrlConn.connect(); //将返回的输入流转换成字符串 InputStream inputStream = httpUrlConn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "GBK"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } bufferedReader.close(); inputStreamReader.close(); //释放资源 inputStream.close(); inputStream = null; httpUrlConn.disconnect(); } catch (Exception e) { return "发起http请求后,获取返回结果失败!"; } return buffer.toString(); } }
讯享网
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/43955.html