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.Google WIFI API アドレス
https://www.googleapis.com/geolocation/v1/geolocate?key=Google キー
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;
}
Wifiアクセスポイント
/**
* WIFI 信息
* @author Mr.li
* @date 2023-06-07
*/
@Data
public class WifiAccessPoint {
private String macAddress;
private Integer signalStrength;
private Integer signalToNoiseRatio;
}
2.3.応答結果エンティティクラス
HTTP結果
/**
* Http 应答
* @author Mr.li
* @date 20220727
*/
@Data
public class HttpResult {
private Integer status;
private String result;
}
Wi-Fiの場所
/**
* 经纬度定位实体类
* @author Mr.li
* @date 20210717
*/
@Data
public class WifiLocation {
/**
* lat lng
*/
private Point location;
private Double accuracy;
}
ポイント
/**
* 经纬度点
* @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.ネットワーク制限
Google API リクエストは通常のリクエストを行うために外国/香港のネットワークを使用する必要があります。そうでない場合、リクエストは通過しません。
3.2. WIFI MAC の数
緯度と経度の情報を要求するには、少なくとも 2 つの WIFI MAC アドレスが必要です。
3.3. 地域の制限
現在、国内の WIFI 情報は位置情報を取得できません。海外の WIFI MAC アドレスのみ位置情報を要求できますが、完全に正確ではない可能性があります。私がテストした現状はこれです。中国で使用されている多数の WIFI MAC アドレスが位置情報を要求できません。 。