通过高德api查询所有店铺地址电话信息
需求:通过高德api查询所有店铺地址信息
需求分析
查询现有高德api发现现有接口关键字搜索API服务地址:
https://developer.amap.com/api/webservice/guide/api/search
参数需要主要参数:key、types、city
其中types city可以查询:
https://developer.amap.com/api/webservice/download
具体实现
1、申请高德appkey
地址:https://console.amap.com/dev/key/app
2、下载types city 字典值
并将字典值解析读取到代码中,如图
3、具体代码调用
类说明:
Api 调用方法
CityUtil 城市编码查询工具类
ReaderFile 文件读写工具类
Shop 地点实体类
ShopUtil 地点查询工具类
具体代码
package com.gaode;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import java.util.ArrayList;
import java.util.List;
/**
* 时间:2024/6/21
* 描述:
*/
public class Api {
/**
* 需要设置自己的apikey
*/
public static String apiKey = "";
public static void main(String[] args) throws Exception{
//查询单个城市
List<Shop> AllShopList = new ArrayList<>();
List<String> types = ReaderFile.getTypes();
List<String> citys = ReaderFile.getCitys();
for (int i = 0; i < types.size(); i++) {
String type = types.get(i);
for (int j = 0; j < citys.size(); j++) {
String city = citys.get(i);
List<Shop> shopList = ShopUtil.queryShops(city,type);
if(CollectionUtil.isNotEmpty(shopList)){
AllShopList.addAll(shopList);
}
if(CollectionUtil.isNotEmpty(AllShopList)){
System.out.println(shopList.size());
System.out.println(shopList);
continue;
}
}
}
System.out.println("结束===================");
System.out.println("=============="+AllShopList.size());
System.out.println(JSON.toJSONString(AllShopList));
}
public static List<Shop> queryAll() {
List<Shop> shopList = new ArrayList<>();
List<String> list = CityUtil.queryCitys();
if (CollectionUtil.isNotEmpty(list)) {
for (String city : list) {
List<Shop> oneShopList = ShopUtil.queryShops(city,null);
if (CollectionUtil.isNotEmpty(oneShopList)) {
shopList.addAll(oneShopList);
}
}
}
return shopList;
}
}
package com.gaode;
/**
* 描述:
*/
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class CityUtil {
public static List<String> queryCitys(){
String apiKey = Api.apiKey;
List<String> ls = new ArrayList<>();
// 获取所有城市列表
String cityListUrl = "https://restapi.amap.com/v3/config/district?key=" + apiKey + "&subdistrict=1";
String cityListResponse = null;
try {
cityListResponse = sendGetRequest(cityListUrl);