码迷,mamicode.com
首页 > Windows程序 > 详细

身份证归属地查询免费api接口代码

时间:2017-05-04 12:09:39      阅读:653      评论:0      收藏:0      [点我收藏+]

标签:name   node   res   parse   open   param   nod   str   elements   

描写叙述 :依据身份证编号 查询归属地信息。

身份证实体类:

package org.wx.xhelper.model;

/**
 * 身份证实体类
 * @author wangxw
 * @version 1.0
 * @date Jul 11, 2014 10:46:54 AM
 */
public class IdCard {

	// 身份证号码
	private String idCard;
	
	// 出生日期
	private String born;
	
	// 性别
	private String sex;
	
	// 所在地区
	private String att;

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}

	public String getBorn() {
		return born;
	}

	public void setBorn(String born) {
		this.born = born;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAtt() {
		return att;
	}

	public void setAtt(String att) {
		this.att = att;
	}
	
	
	
}


服务接口类:

package org.wx.xhelper.service;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.wx.xhelper.model.IdCard;

/**
 * 身份证信息服务接口类
 * @author wangxw
 * @version 1.0
 * @date Jul 11, 2014 10:49:57 AM
 */
public class IdCardService {
	
	/**
	 * 生成身份证信息
	 * @param cardNo
	 * @return 返回身份证信息
	 * @throws UnsupportedEncodingException
	 */
	public static String getIdCardDetail(String cardNo) throws UnsupportedEncodingException{
		// 获取身份证信息
		IdCard idcard = getIdCardInfo(cardNo);
		
		// 存储文本信息
		StringBuffer news = new StringBuffer();
		
		if (idcard != null) {
			news.append("所属地区:"+idcard.getAtt()).append("\n");
			news.append("出生日期:"+idcard.getBorn()).append("\n");
			news.append("性别:"+idcard.getSex()).append("\n");
		}
		
		if(news.length() == 0){
			news.append("身份证号码").append(cardNo).append("不存在,请又一次输入!");
		}
		
		return news.toString();
	}
	
	/**
	 * 获取身份证信息
	 * @param cardNo
	 * @return 返回身份证信息
	 */
	public static IdCard getIdCardInfo(String cardNo){
		URL url = null;
		IdCard idCard = new IdCard();
		try{
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder();
			
			url = new URL("http://api.k780.com:88/?

app=idcard.get&idcard="+cardNo+"&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml"); System.out.println(url); Document doc = builder.parse(url.openStream()); NodeList node = doc.getElementsByTagName("result"); for(int i=0;i<node.getLength();i++){ String idcard = ""; String born = ""; String sex = ""; String att = ""; if(doc.getElementsByTagName("idcard").item(i).getFirstChild() != null){ idcard = doc.getElementsByTagName("idcard").item(i).getFirstChild().getNodeValue(); } if(doc.getElementsByTagName("born").item(i).getFirstChild() != null){ born = doc.getElementsByTagName("born").item(i).getFirstChild().getNodeValue(); } if(doc.getElementsByTagName("sex").item(i).getFirstChild() != null){ sex = doc.getElementsByTagName("sex").item(i).getFirstChild().getNodeValue(); } if(doc.getElementsByTagName("att").item(i).getFirstChild() != null){ att = doc.getElementsByTagName("att").item(i).getFirstChild().getNodeValue(); } idCard.setIdCard(idcard); idCard.setBorn(born); idCard.setSex(sex); idCard.setAtt(att); } }catch(Exception e){ e.printStackTrace(); } return idCard; } public static void main(String[] args){ try { System.out.print(getIdCardDetail("110101199001011118")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }


返回结果:

所属地区:北京市东城区
出生日期:1990年01月01日
性别:男

身份证归属地查询免费api接口代码

标签:name   node   res   parse   open   param   nod   str   elements   

原文地址:http://www.cnblogs.com/wzjhoutai/p/6806169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!