Java to implement ID attribution query

Source: Internet
Author: User

The interface of the aggregated web site is used. The Gson library used for JSON parsing. : http://download.csdn.net/detail/bjq1016/8225447

Two x 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 ();     }}

Query class, get method request data:

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 ;p ublic 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 ;    }}

Test class:

Package com.itfanr.idcard;/** * Created by Itfanr on 14/11/23. */import Com.google.gson.Gson;p ublic class Test {public static void main (String args[]) {Search s = new Sea        RCH ("Your_key");        String result = S.seatchid ("330326198903081211");        Gson Gson = new Gson ();        Resultinfo Resultinfo = Gson.fromjson (result, resultinfo.class);        ;    System.out.println (Resultinfo); }}

Output Result:

resutltinfo-> resultcode:203 reason: ID check bit incorrect result:person info-> area: Zhejiang Wenzhou Pingyang County birthday:1989 year March 08 sex: Man ve Rify: The ID number check bit is incorrect error_code:203803


Java to implement ID attribution query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.