点击功能进行复制代码,就拿百度的身份证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"); // 填入apikey到HTTP 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
?
接下来就是切割字符串了
- 将Unicode转为汉字返回
public static String convert(String utfString){ ????????StringBuilder sb = new StringBuilder(); ????????int ????????int ???????? ????????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 ????} |