看了一点点皮毛、就迫不及待的想试试。
前阵子有写过一篇Ajax查询手机号码归属地其中就用到了网络编程的URL类跟URLConnection类。
但是我并不了解这两个类、不过也不妨碍我copy啊、嘿嘿。
今天来补上它。通过查询身份证号码:
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.URL;
- /**
- *
- * @author 古道西风
- *
- */
- public class URLConnection {
- /**
- * @param args
- */
- public static void main(String[] args) {
- try{
- /*
- * 有道身份证查询API
- * 注意:不同的API返回的数据也是不同的、需要自己处理一下
- * 返回JSON数据:updateCall(1, {‘product’:'identitycard’,'code’:’321528198807303319′,’location’:'湖北省宜昌市长阳土家族自治县’,'birthday’:’19880730′,’gender’:'m’} , ”);
- * 原文地址:http://www.hujuntao.com/archives/youdao-idcard-api.html
- * 查询IP:http://www.yodao.com/smartresult-xml/search.s?type=ip&q=21.123.123.123
- 可以查到IP对应的物理地址及网络服务商
- */
- //String url = "http://www.youdao.com/smartresult-xml/search.s?jsFlag=true&type=id&q=";
- String url = "http://chaxun.1616.net/s.php?type=idcard&v=522624199107274414&output=json&callback=J1616.chaxun.shenfenzheng.callback";
- String result = callUrlByGet(url,"UTF-8");
- String[] strArray = result.split(",");
- for (int i = 0; i < strArray.length -1; i++) {
- String[] arrays = strArray[i].split(":");
- String title = arrays[0].substring(arrays[0].indexOf("\"")+1,arrays[0].length()-1);
- if(title.equals("IdCard")){
- title ="身份证号";
- }else if (title.equals("Sex")){
- title ="性别";
- }else if (title.equals("BirthDay")){
- title ="出生日期";
- }else if (title.equals("City")){
- title ="发证地";
- }
- System.out.println(title+":"+arrays[1].substring(arrays[1].indexOf("\"")+1,arrays[1].length()-1));
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- /**
- *
- * @param callurl URL链接
- * @param charset 编码方式
- * @return
- */
- public static String callUrlByGet(String callurl,String charset){
- /*
- * 返回字符串
- */
- String result = "";
- try {
- URL url = new URL(callurl); //构建一个URL对象
- /*
- * 调用URL类中的openConnection方法获得URLConnection对象
- */
- java.net.URLConnection connection = url.openConnection();
- /*调用connect方法链接远程资源*/
- connection.connect();
- /*
- * 使用getInputStream方法获取一个输入流、用以读取信息(这个输入流
- * 与URL类中的openStream方法所返回的流相同)
- *
- * 构造一个InputStreamReader流对象
- *
- * 构造一个字符输入流BufferedReader
- *
- * 为什么不直接用 InputStreamReader流对象呢、JDK的API有介绍:
- * InputStreamReader 是字节流通向字符流的桥梁:
- * 它使用指定的 charset 读取字节并将其解码为字符。
- * 它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。
- * 每次调用 InputStreamReader 中的一个 read()
- * 方法都会导致从底层输入流读取一个或多个字节。要启用从字节到字符的有效转换,
- * 可以提前从底层流读取更多的字节,使其超过满足当前读取操作所需的字节。
- * 为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。例如:
- * BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- */
- BufferedReader reader = new BufferedReader(new
- InputStreamReader(connection.getInputStream(),charset));
- String line;
- /*
- * 循环读取
- */
- while((line = reader.readLine())!= null){
- result += line;
- result += "\n";
- }
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
- /*
- * 在这之前、你可以先输出看看、返回的字符串的数据格式、
- * 然后在处理掉你不想要的数据、留下你想要的信息
- */
- if(result!=null&&!"".equals(result)){
- result = result.substring(result.indexOf("{"+"")+1, (result.indexOf("}")) );
- }
- return result;
- }
- }