采用的是聚合网站的接口。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
|