package com.chinamobile.cmss.share.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class Address {
/**
* 功能说明:更新高德的省市区到我们的省市区表里
* @author MrDoubi 2019-07-08 10:12:30
* @return model
*/
public static void updateInitGaoDe(){
//慎用!
//我本人用来删除省市区表原来的记录的
//高德的key 申请时选择web服务
String gaodeKey = "";
//一个http请求 这个方法放在羡慕
String res =
HttpClientUtils.jsonPostRequest("http://restapi.amap.com/v3/config/district","key=高德地图注册的key值&subdistrict=3");
JSONObject json = JSONObject.parseObject(res);
int count = 0;
System.out.println(json.get("status"));
List list = new ArrayList<>();
Map provinceMap = new HashMap();
if(json.get("status").equals("1")){
//国家 暂时只有中国
JSONArray districts = json.getJSONArray("districts");
for (int i = 0; i < districts.size(); i++) {
JSONObject district = districts.getJSONObject(i);
//省份
JSONArray provinces = district.getJSONArray("districts");
for (int j = 0; j < provinces.size(); j++) {
JSONObject province = provinces.getJSONObject(j);
// System.out.println(
// "province :::::: ==== name:"+province.getString("name")+
// " center:"+province.getString("center")+
// " adcode:"+province.getString("adcode")
// );
//在这里新增省份 到表中
//市
JSONArray citys = province.getJSONArray("districts");
Map cityMap = new HashMap();
for (int k = 0; k < citys.size(); k++) {
JSONObject city = citys.getJSONObject(k);
// System.out.println(
// "city :::::: ==== name:"+city.getString("name")+
// " center:"+city.getString("center")+
// " adcode:"+city.getString("adcode")
// );
// System.out.println(province.getString("name") + "-" + city.getString("name"));
//在这里新增市 到表中
//区
JSONArray areas = city.getJSONArray("districts");
Map countyMap = new HashMap();
for (int l = 0; l < areas.size(); l++) {
JSONObject area = areas.getJSONObject(l);
countyMap.put(area.getString("name"), area.getString("name"));
count++;
// list.add(province.getString("name") + "-" + city.getString("name") + "-" + area.getString("name"));
// System.out.println(province.getString("name") + "-" + city.getString("name") + "-" + area.getString("name"));
/*System.out.println(
"area :::::: ==== name:"+area.getString("name")+
" center:"+area.getString("center")+
" adcode:"+area.getString("adcode")
);*/
//在这里新增区 到表中
}
cityMap.put(city.getString("name"), countyMap);
}
provinceMap.put(province.getString("name"), cityMap);
}
}
List result = new ArrayList();
List proList = sortData(provinceMap);
count = 0;
for (String pro : proList) {
if (provinceMap.get(pro) instanceof Map) {
Map map = (Map) provinceMap.get(pro);
List ciList = sortData(map);
for (String ci : ciList) {
// System.out.println(pro + "-" + ci);
Map map2 = (Map) map.get(ci);
List coList = sortData(map2);
for (String co : coList) {
System.out.println(pro + "-" + ci + "-" + co);
count++;
}
}
}
}
System.out.println("获取成功 共"+count+"条数据");
}else {
System.err.println(json.get("info"));
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static List sortData(Map map) {
Set set = map.keySet();
Object[] ls = set.toArray();
List list = new ArrayList<>();
for (Object str : ls) {
list.add(str.toString());
}
@SuppressWarnings("rawtypes")
Comparator comparator = Collator.getInstance(java.util.Locale.CHINA);
Collections.sort(list,comparator);
return list;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void sortData(List list) {
@SuppressWarnings("rawtypes")
Comparator comparator = Collator.getInstance(java.util.Locale.CHINA);
Collections.sort(list,comparator);
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param)
{
PrintWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try
{
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("contentType", "utf-8");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null)
{
result.append(line);
}
}
catch (ConnectException e)
{
}
catch (SocketTimeoutException e)
{
}
catch (IOException e)
{
}
catch (Exception e)
{
}
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (IOException ex)
{
}
}
return result.toString();
}
public static void main(String[] args) {
updateInitGaoDe();
}
}