从第三方api获取行政区划市级以上数据

获取市及以上行政区划数据

简介:需要为移动端提供市及以上区域字典信息,采取httpclient的方式访问第三方的数据接口获取解析。

环境配置

  • jdk1.8
  • idea2016.2.4
  • Maven3.3.9

第三方架包

  • HttpClient
  • fastjson

创建配置maven quick-start项目

新建maven工程,选择quick-start

从第三方api获取行政区划市级以上数据_第1张图片

配置GroupId和Artifactd

从第三方api获取行政区划市级以上数据_第2张图片

设置maven属性(如果有maven环境默认即可)

从第三方api获取行政区划市级以上数据_第3张图片

设置工程目录

从第三方api获取行政区划市级以上数据_第4张图片

配置项目属性

添加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>

添加fastJson依赖

<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>

获取数据来源的API

我的数据来源于极速数据(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;

创建区域信息实体(get / set省略)

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

创建HttpUtils工具类

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

获取拼音转换工具类

  • 参考地址:http://blog.csdn.net/top_code/article/details/39641615

完善main方法

    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);
        }
    }

控制台结果如下,复制sql语句执行到数据库完成数据获取。
从第三方api获取行政区划市级以上数据_第5张图片

你可能感兴趣的:(数据获取)