Java操作百度身份证API

网址:http://apistore.baidu.com/

点击功能进行复制代码,就拿百度的身份证API 举例子:

http://apistore.baidu.com/apiworks/servicedetail/113.html

 

Java 代码:

String httpUrl = "http://apis.baidu.com/apistore/idservice/id";

String httpArg = "id=420984198704207896";

String jsonResult = request(httpUrl, httpArg);

System.out.println(jsonResult);

 

/**

* @param urlAll

* :请求接口

* @param httpArg

* :参数

* @return 返回结果

*/

public static String request(String httpUrl, String httpArg) {

BufferedReader reader = null;

String result = null;

StringBuffer sbf = new StringBuffer();

httpUrl = httpUrl + "?" + httpArg;

 

try {

URL url = new URL(httpUrl);

HttpURLConnection connection = (HttpURLConnection) url

.openConnection();

connection.setRequestMethod("GET");

// 填入apikeyHTTP header

connection.setRequestProperty("apikey", "您自己的apikey");

connection.connect();

InputStream is = connection.getInputStream();

reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

String strRead = null;

while ((strRead = reader.readLine()) != null) {

sbf.append(strRead);

sbf.append("\r\n");

}

reader.close();

result = sbf.toString();

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

填入自己的apikey

 

接下来就是切割字符串了

  1. 将Unicode转为汉字返回

public static String convert(String utfString){

        StringBuilder sb = new StringBuilder();

        int i = -1;

        int pos = 0;

        

        while((i=utfString.indexOf("\\u", pos)) != -1){

            sb.append(utfString.substring(pos, i));

            if(i+5 < utfString.length()){

                pos = i+6;

                sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));

            }

        }

        

        return sb.toString();

    }

你可能感兴趣的:(java)