简介:需要为移动端提供市及以上区域字典信息,采取httpclient的方式访问第三方的数据接口获取解析。
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.4.1version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpmimeartifactId>
<version>4.4.1version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>fluent-hcartifactId>
<version>4.4.1version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpcoreartifactId>
<version>4.4.1version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.4.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.4.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.modulegroupId>
<artifactId>jackson-module-jaxb-annotationsartifactId>
<version>2.4.0version>
dependency>
我的数据来源于极速数据(http://www.jisuapi.com)的行政区划接口,可免费调用100次,其余第三方接口都可以获得,获取解析后存储到自己的数据库使用。
-请求参数
名称 | 类型 | 是否必填 | 说明 |
---|---|---|---|
appkey | String | 是 | 注册后分配的密钥 |
parentid | int | 否 | 上级id |
-响应参数
名称 | 类型 | 说明 |
---|---|---|
id | int | ID |
parentid | int | 上级id |
areacode | string | 区号 |
zipcode | string | 邮编 |
depth | string | 邮区域等级(深度) 冗余字段,用来查找 |
DROP TABLE IF EXISTS `fm_city`;
CREATE TABLE `fm_city` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '城市id',
`name` varchar(40) DEFAULT NULL COMMENT '城市名称',
`parent_id` bigint(20) DEFAULT NULL COMMENT '上级id',
`parent_name` varchar(40) DEFAULT NULL COMMENT '上级名称',
`area_code` varchar(10) DEFAULT NULL COMMENT '区号',
`zipcode` varchar(10) DEFAULT NULL COMMENT '邮编',
`depth` varchar(1) DEFAULT NULL COMMENT '区域等级(深度)',
`pinyin_code` varchar(40) DEFAULT NULL COMMENT '区域全拼',
`first_char` varchar(1) DEFAULT NULL COMMENT '拼音大写首字母',
`is_hot` varchar(1) DEFAULT NULL COMMENT '是否热门城市',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=393 DEFAULT CHARSET=utf8mb4;
public class Area {
private Integer id;
private String name;
private Integer parentid;
private String parentname;
private String areaCode;
private String zipCode;
private String depth;
private String pinYinCode;
private String firstChar;
}
为了方便打印出sql语句,重写toString()方法:
@Override
public String toString() {
StringBuffer buffer =
new StringBuffer("INSERT INTO `fm_city`(`id`, `name`, `parent_id`, `parent_name`, `area_code`, `zipcode`, `depth`, `pinyin_code`, `first_char`) VALUES (");
buffer.append(this.getId() + ",");
buffer.append("'" + this.getName() + "',");
buffer.append(this.getParentid() + ",");
buffer.append("'" + this.getParentname() + "',");
buffer.append("'" + this.getAreaCode() + "',");
buffer.append("'" + this.getZipCode() + "',");
buffer.append("'" + this.getDepth() + "',");
buffer.append("'" + this.getPinYinCode() + "',");
buffer.append("'" + this.getFirstChar() + "')");
return buffer.toString();
}
package com.qzj.util;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/**
* Created by qzj on 2016/11/2.
*/
public class HttpUtils {
/**
* 构建httpClient对象
*
* @return
*/
public static CloseableHttpClient createClientDefault() {
return HttpClients.createDefault();
}
/**
* 获取请求的响应结果
*
* @param httpClient
* @param requestBase
* @param context
* @return
*/
public static String getResponseText(CloseableHttpClient httpClient, HttpRequestBase requestBase, HttpContext context) {
String responseText = null;
try {
CloseableHttpResponse response = httpClient.execute(requestBase, context);
try {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
long len = httpEntity.getContentLength();
responseText = EntityUtils.toString(httpEntity);
}
} finally {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return responseText;
}
}
public class AreaInfoGet {
private static final String APP_KEY = "你的appKey";
private static final String QUEERY_URL = "http://api.jisuapi.com/area/query?appkey=" + APP_KEY;
private ObjectMapper mapper = new ObjectMapper();
CloseableHttpClient client = HttpUtils.createClientDefault();
HttpClientContext context = HttpClientContext.create();
/**
* 查询获取数据
*
* @param paramMap 参数map
* @return
* @throws IOException
*/
public List<Area> query(Map<String, String> paramMap) throws IOException {
String queryUrl = QUEERY_URL;
for (String key : paramMap.keySet()) {//拼装参数
queryUrl = queryUrl + "&" + key + "=" + paramMap.get(key);
}
HttpGet getInfo = new HttpGet(queryUrl);
String info = HttpUtils.getResponseText(client, getInfo, context);
Map<String, Object> resultMap = mapper.readValue(info, Map.class);
List<Map<String, String>> list = (List<Map<String, String>>) resultMap.get("result");
return this.ListMap2ListArea(list);
}
/**
* 转换方法
*
* @param list
* @return
*/
private List<Area> ListMap2ListArea(List<Map<String, String>> list) {
List<Area> results = new ArrayList<Area>();
for (Map<String, String> map : list) {
Area area = new Area();
area.setId(Integer.parseInt(map.get("id")));
area.setName(map.get("name"));
area.setParentid(Integer.parseInt(map.get("parentid")));
area.setParentname(map.get("parentname"));
area.setAreaCode(map.get("areacode"));
area.setZipCode(map.get("zipcode"));
area.setDepth(map.get("depth"));
results.add(area);
}
return results;
}
}
/**
* 特殊地区不获取下级地区
*
* @param name
* @return
*/
public static boolean checkName(String name) {
String[] city = {"北京", "天津", "重庆", "上海", "香港", "澳门", "台湾", "国外"};
for (String str : city) {
if (str.equals(name)) {
return false;
}
}
return true;
}
public static void main(String[] args) throws IOException, BadHanyuPinyinOutputFormatCombination {
AreaInfoGet get = new AreaInfoGet();
List results = new ArrayList();
List rootCity = get.query(new HashMap());
results.addAll(rootCity);
for (Area area : rootCity) {//循环获取子区域
if (checkName(area.getName())) {
Map param = new HashMap();
param.put("parentid", area.getId() + "");
List citys = get.query(param);
results.addAll(citys);
}
}
for (Area area : results) {//循环转换拼音,设置
String pinyinCode = PinyinUtils.chineseToPinYinF(area.getName());
area.setPinYinCode(pinyinCode);
area.setFirstChar(pinyinCode.substring(0, 1).toUpperCase());
System.out.println(area);
}
}