1.创建Http请求工具类
1.1.引入httpclient
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
1.2.封装Http工具类
/**
* Http请求
* @author Nr.li
* @date 20220727
*/
@Slf4j
public class HttpUtils {
private HttpUtils() {
}
private static CloseableHttpClient httpClient = HttpClientBuilder.create().build();
@Autowired
private static RequestConfig config;
/**
* 发送Http Post请求
* @param url
* @param paramsJson
* @param headsMap
* @return
* @throws IOException
*/
public static HttpResult httpPost(String url, String paramsJson, Map<String, String> headsMap) throws IOException {
HttpResult httpResult = new HttpResult();
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//请求头
if (headsMap != null && !headsMap.isEmpty()) {
headsMap.forEach((key, value) -> {
httpPost.addHeader(key, value);
});
}
if(headsMap==null){
headsMap=new HashMap<>();
}
if (!headsMap.containsKey("Content-type")) {
httpPost.addHeader("Content-type", "application/json;charset=utf-8");
}
StringEntity stringEntity = new StringEntity(paramsJson, "UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity result = response.getEntity();
String resultStr = null;
if (result != null) {
resultStr = EntityUtils.toString(result, "UTF-8");
}
httpClient.close();
response.close();
httpResult.setStatus(HttpStatus.SC_OK);
httpResult.setResult(resultStr);
return httpResult;
} else {
httpResult.setStatus(response.getStatusLine().getStatusCode());
httpResult.setResult("");
return httpResult;
}
}catch (Exception ex) {
httpResult.setStatus(HttpStatus.SC_NOT_FOUND);
httpResult.setResult(ex.getMessage());
return httpResult;
}
}
/**
* 不带参数的get请求,如果状态码为200,则返回body,如果不为200,则返回null
*
* @param url
* @return
* @throws Exception
*/
public static String doGet(String url,Map<String,String> headerMap) {
try {
// 声明 http get 请求
HttpGet httpGet = new HttpGet(url);
if(headerMap!=null) {
for (String key : headerMap.keySet()) {
httpGet.addHeader(key, headerMap.get(key));
}
}
// 装载配置信息
httpGet.setConfig(config);
// 发起请求
CloseableHttpResponse response = httpClient.execute(httpGet);
if(response!=null) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}else {
return null;
}
}catch (Exception e) {
return null;
}
}
}
2.请求API地址
2.1.谷歌WIFI API地址
https://www.googleapis.com/geolocation/v1/geolocate?key=你的谷歌key
2.2.请求参数实体类
WifiParams
/**
* WIFI定位请求参数
* @author Mr.li
* @date 2023-06-07
*/
@Data
public class WifiParams {
/**
* 当WIFI无法定位的时候,是否采用IP定位,这里最好不要开启,设置为false
*/
private boolean considerIp;
private List<WifiAccessPoint> wifiAccessPoints;
}
WifiAccessPoint
/**
* WIFI 信息
* @author Mr.li
* @date 2023-06-07
*/
@Data
public class WifiAccessPoint {
private String macAddress;
private Integer signalStrength;
private Integer signalToNoiseRatio;
}
2.3.应答结果实体类
HttpResult
/**
* Http 应答
* @author Mr.li
* @date 20220727
*/
@Data
public class HttpResult {
private Integer status;
private String result;
}
WifiLocation
/**
* 经纬度定位实体类
* @author Mr.li
* @date 20210717
*/
@Data
public class WifiLocation {
/**
* lat lng
*/
private Point location;
private Double accuracy;
}
Point
/**
* 经纬度点
* @author Mr.li
* @date 2023-06-07
*/
@Data
public class Point {
private Double lat;
private Double lng;
}
2.4.请求方法示例
public static void main(String[] args) {
String url="https://www.googleapis.com/geolocation/v1/geolocate?key=你的谷歌key";
WifiParams wifiParams=new WifiParams();
List<WifiAccessPoint> wifiAccessPointList=new ArrayList<>();
WifiAccessPoint accessPoint=new WifiAccessPoint();
accessPoint.setMacAddress("e8:2c:6d:0f:b5:54");
accessPoint.setSignalStrength(-59);
accessPoint.setSignalToNoiseRatio(0);
wifiAccessPointList.add(accessPoint);
WifiAccessPoint accessPoint2=new WifiAccessPoint();
accessPoint2.setMacAddress("ec:ad:e0:4e:cd:20");
accessPoint2.setSignalStrength(-68);
accessPoint2.setSignalToNoiseRatio(0);
try {
HttpResult httpResult = HttpUtils.httpPost(url, JSON.toJSONString(wifiParams), null);
if (httpResult.getStatus() == 200) {
String result = httpResult.getResult();
WifiLocation wifiLocation = JSON.parseObject(result, WifiLocation.class);
System.out.println(wifiLocation);
}
}catch (Exception e){
log.error("WiFi定位请求异常:",e);
}
}
3.特别说明
3.1.网络限制
谷歌API请求,必须使用国外/香港网络才能正常请求,否则是无法请求通过。
3.2.WIFI MAC数量
至少需要两个WIFI的MAC地址方可请求到经纬度信息
3.3.区域限制
目前国内WIFI信息无法进行定位,只有国外的WIFI MAC地址才能请求到位置,不过也不一定完全准确,目前我测试的情况是这样,国内使用了大量的WIFI MAC地址都无法请求到位置。