java网络编程Socket学习(二)

看了一点点皮毛、就迫不及待的想试试。

前阵子有写过一篇Ajax查询手机号码归属地其中就用到了网络编程的URL类跟URLConnection类。

但是我并不了解这两个类、不过也不妨碍我copy啊、嘿嘿。

今天来补上它。通过查询身份证号码:

  1. import java.io.BufferedReader;  
  2.   
  3. import java.io.InputStreamReader;  
  4. import java.net.URL;  
  5.   
  6. /** 
  7.  *  
  8.  * @author 古道西风 
  9.  * 
  10.  */  
  11. public class URLConnection {  
  12.   
  13.     /** 
  14.      * @param args 
  15.      */  
  16.     public static void main(String[] args) {  
  17.         try{  
  18.             /* 
  19.              * 有道身份证查询API  
  20.              * 注意:不同的API返回的数据也是不同的、需要自己处理一下 
  21.              * 返回JSON数据:updateCall(1, {‘product’:'identitycard’,'code’:’321528198807303319′,’location’:'湖北省宜昌市长阳土家族自治县’,'birthday’:’19880730′,’gender’:'m’} , ”); 
  22.              * 原文地址:http://www.hujuntao.com/archives/youdao-idcard-api.html 
  23.              * 查询IP:http://www.yodao.com/smartresult-xml/search.s?type=ip&q=21.123.123.123 
  24.                 可以查到IP对应的物理地址及网络服务商 
  25.              */  
  26.             //String url = "http://www.youdao.com/smartresult-xml/search.s?jsFlag=true&type=id&q=";  
  27.             String url = "http://chaxun.1616.net/s.php?type=idcard&v=522624199107274414&output=json&callback=J1616.chaxun.shenfenzheng.callback";    
  28.             String result = callUrlByGet(url,"UTF-8");   
  29.             String[] strArray = result.split(",");  
  30.             for (int i = 0; i < strArray.length -1; i++) {  
  31.                 String[] arrays = strArray[i].split(":");  
  32.                 String title = arrays[0].substring(arrays[0].indexOf("\"")+1,arrays[0].length()-1);  
  33.                 if(title.equals("IdCard")){  
  34.                     title ="身份证号";  
  35.                 }else if (title.equals("Sex")){  
  36.                     title ="性别";  
  37.                 }else if (title.equals("BirthDay")){  
  38.                     title ="出生日期";  
  39.                 }else if (title.equals("City")){   
  40.                     title ="发证地";  
  41.                 }  
  42.                 System.out.println(title+":"+arrays[1].substring(arrays[1].indexOf("\"")+1,arrays[1].length()-1));  
  43.             }  
  44.               
  45.         }catch(Exception e){   
  46.             e.printStackTrace();   
  47.         }  
  48.     }  
  49.       
  50.     /** 
  51.      *  
  52.      * @param callurl URL链接 
  53.      * @param charset 编码方式 
  54.      * @return 
  55.      */  
  56.     public static String callUrlByGet(String callurl,String charset){  
  57.         /* 
  58.          * 返回字符串 
  59.          */  
  60.         String result = "";      
  61.         try {      
  62.             URL url = new URL(callurl); //构建一个URL对象     
  63.             /* 
  64.              * 调用URL类中的openConnection方法获得URLConnection对象 
  65.              */  
  66.             java.net.URLConnection connection = url.openConnection();  
  67.             /*调用connect方法链接远程资源*/  
  68.             connection.connect();   
  69.             /* 
  70.              * 使用getInputStream方法获取一个输入流、用以读取信息(这个输入流 
  71.              * 与URL类中的openStream方法所返回的流相同) 
  72.              *  
  73.              * 构造一个InputStreamReader流对象 
  74.              *  
  75.              * 构造一个字符输入流BufferedReader 
  76.              *  
  77.              * 为什么不直接用 InputStreamReader流对象呢、JDK的API有介绍: 
  78.              * InputStreamReader 是字节流通向字符流的桥梁: 
  79.              * 它使用指定的 charset 读取字节并将其解码为字符。 
  80.              * 它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。  
  81.              * 每次调用 InputStreamReader 中的一个 read()  
  82.              * 方法都会导致从底层输入流读取一个或多个字节。要启用从字节到字符的有效转换, 
  83.              * 可以提前从底层流读取更多的字节,使其超过满足当前读取操作所需的字节。   
  84.              * 为了达到最高效率,可要考虑在 BufferedReader 内包装 InputStreamReader。例如:  
  85.              * BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
  86.              */  
  87.             BufferedReader reader = new BufferedReader(new     
  88.                     InputStreamReader(connection.getInputStream(),charset));      
  89.             String line;  
  90.             /* 
  91.              * 循环读取 
  92.              */  
  93.             while((line = reader.readLine())!= null){       
  94.                 result += line;      
  95.                 result += "\n";      
  96.             }      
  97.         } catch (Exception e) {      
  98.             e.printStackTrace();      
  99.             return "";        
  100.         }  
  101.         /* 
  102.          * 在这之前、你可以先输出看看、返回的字符串的数据格式、 
  103.          * 然后在处理掉你不想要的数据、留下你想要的信息 
  104.          */  
  105.         if(result!=null&&!"".equals(result)){    
  106.             result = result.substring(result.indexOf("{"+"")+1, (result.indexOf("}")) );      
  107.         }    
  108.         return result;        
  109.     }   
  110.   
  111. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值