java实现身份证归属地查询

采用的是聚合网站的接口。json解析用的Gson库。下载地址:http://download.csdn.net/detail/bjq1016/8225447

两个JavaBean:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package  com.itfanr.IDCard;
 
/**
  * Created by itfanr on 14/12/4.
  */
public  class  ResultInfo {
 
     private  int  resultcode ;
     private   String reason ;
 
     public  Person getResult() {
         return  result;
     }
 
     public  void  setResult(Person result) {
         this .result = result;
     }
 
     private  Person result ;
     private  int  error_code ;
 
     public  int  getResultcode() {
         return  resultcode;
     }
 
     public  void  setResultcode( int  resultcode) {
         this .resultcode = resultcode;
     }
 
     public  String getReason() {
         return  reason;
     }
 
     public  void  setReason(String reason) {
         this .reason = reason;
     }
 
 
     public  int  getError_code() {
         return  error_code;
     }
 
     public  void  setError_code( int  error_code) {
         this .error_code = error_code;
     }
 
   public  String toString(){
       return  "ResutltInfo-> " "resultcode: " this .getResultcode()
               + " reason: " + this .getReason()+ "\n" + "result: " this .getResult()
               + "\n" + "error_code: " this .getError_code() ;
   }
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package  com.itfanr.IDCard;
 
/**
  * Created by itfanr on 14/12/4.
  */
public  class  Person {
     private  String area ;
     private  String sex ;
     private   String birthday ;
     private  String verify;
 
     public  String getVerify() {
         return  verify;
     }
 
     public  void  setVerify(String verify) {
         this .verify = verify;
     }
 
 
     public  String getArea() {
         return  area;
     }
 
     public  void  setArea(String area) {
         this .area = area;
     }
 
     public  String getSex() {
         return  sex;
     }
 
     public  void  setSex(String sex) {
         this .sex = sex;
     }
 
     public  String getBirthday() {
         return  birthday;
     }
 
     public  void  setBirthday(String birthday) {
         this .birthday = birthday;
     }
 
     public  String toString(){
         return  "Person info-> " "area: " this .getArea()
                 " birthday: "  this .getBirthday()
                 + " sex: " this .getSex() +  " verify: " + this .getVerify();
     }
}

查询类,get方式请求数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package  com.itfanr.IDCard;
 
/**
  * Created by itfanr on 14/12/4.
  */
 
import  java.io.ByteArrayOutputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.net.MalformedURLException;
import  java.net.URL ;
import   java.net.HttpURLConnection ;
public  class  Search {
 
     private  String key ;
     public  Search(String key ) {
         this .key =key ;
 
     }
 
     public   String seatchID(String id){
         String url =  "http://apis.juhe.cn/idcard/index?key=" + this .key+  "&cardno=" + id ;
         URL urlNet =  null  ;
         InputStream is =  null  ;
         ByteArrayOutputStream bao =  null  ;
         String result =  null  ;
         try  {
             urlNet =  new  URL(url);
             try  {
                 HttpURLConnection conn = (HttpURLConnection)urlNet.openConnection() ;
                 conn.setReadTimeout( 5 * 1000  );
                 conn.setRequestMethod( "GET" );
                  is = conn.getInputStream() ;
                 int  len = - 1  ;
                 byte [] buf =  new  byte [ 128 ] ;
                 bao =  new  ByteArrayOutputStream() ;
                 while  ((len = is.read(buf))!=- 1 ){
                     bao.write(buf, 0 ,len);
 
                 }
                 bao.flush();
                 result =  new  String(bao.toByteArray()) ;
             catch  (IOException e) {
                 e.printStackTrace();
             }
         catch  (MalformedURLException e) {
             e.printStackTrace();
         } finally  {
             if  (is!= null ){
                 try  {
                     is.close();
                 catch  (IOException e) {
                     e.printStackTrace();
                 }
             }
             if  (bao!= null ){
                 try  {
                     bao.close();
                 catch  (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
 
         return  result ;
 
     }
}

测试类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package  com.itfanr.IDCard;
 
/**
  * Created by itfanr on 14/11/23.
  */
import   com.google.gson.Gson ;
public  class  Test {
     public   static  void   main(String args[]){
 
         Search s =  new  Search( "your_key"  ) ;
         String result = s.seatchID( "330326198903081211" );
 
         Gson gson =  new  Gson() ;
         ResultInfo resultInfo  = gson.fromJson(result, ResultInfo. class );
         ;
         System.out.println(resultInfo);
 
     }
 
}

输出结果:

?
1
2
3
ResutltInfo-> resultcode:  203  reason: 身份证校验位不正确
result: Person info-> area: 浙江省温州市平阳县 birthday:  1989 03 08 日 sex: 男 verify: 该身份证号校验位不正确
error_code:  203803

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值