首页 > 代码库 > java实现身份证归属地查询
java实现身份证归属地查询
采用的是聚合网站的接口。json解析用的Gson库。下载地址:http://download.csdn.net/detail/bjq1016/8225447
两个JavaBean:
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() ; } }
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方式请求数据:
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 ; } }
测试类:
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); } }
输出结果:
ResutltInfo-> resultcode: 203 reason: 身份证校验位不正确 result: Person info-> area: 浙江省温州市平阳县 birthday: 1989年03月08日 sex: 男 verify: 该身份证号校验位不正确 error_code: 203803
java实现身份证归属地查询
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。