79 lines
3.0 KiB
Java
79 lines
3.0 KiB
Java
|
package com.zcloud.service.map.util;
|
||
|
|
||
|
import com.alibaba.fastjson.JSON;
|
||
|
import com.zcloud.entity.PageData;
|
||
|
import com.zcloud.util.UuidUtil;
|
||
|
import org.apache.http.HttpEntity;
|
||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||
|
import org.apache.http.client.methods.HttpPost;
|
||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||
|
import org.apache.http.impl.client.HttpClients;
|
||
|
import org.apache.http.util.EntityUtils;
|
||
|
import org.springframework.stereotype.Component;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.io.UnsupportedEncodingException;
|
||
|
import java.net.URLEncoder;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
* @author fangjiakai
|
||
|
* @date 2023/09/08 11:32
|
||
|
*/
|
||
|
@Component
|
||
|
public class WeatherUtil {
|
||
|
private static final String WEATHER_PATH = "http://yunlink.027010.cn/OpenAPI/GetAllReal";
|
||
|
private static final String sn="01803353066";
|
||
|
private static final String pwd="353066";
|
||
|
|
||
|
public static Map<String,Object> getWeatherInfo() throws IOException {
|
||
|
CloseableHttpClient client = null;
|
||
|
CloseableHttpResponse response = null;
|
||
|
HttpPost httpPost = new HttpPost(buildUrl(WEATHER_PATH,sn,pwd));
|
||
|
|
||
|
client = HttpClients.createDefault();
|
||
|
response = client.execute(httpPost);
|
||
|
HttpEntity entity = response.getEntity();
|
||
|
String result = EntityUtils.toString(entity);
|
||
|
return JSON.parseObject(result, HashMap.class);//返回结果转换为map
|
||
|
}
|
||
|
|
||
|
private static String buildUrl(String uri,String sn,String pwd) {
|
||
|
StringBuilder urlBuilder = new StringBuilder(uri+"?");
|
||
|
try {
|
||
|
urlBuilder.append("sn=").append(URLEncoder.encode(sn, "UTF-8"));
|
||
|
urlBuilder.append("&pwd=").append(URLEncoder.encode(pwd, "UTF-8"));
|
||
|
} catch (UnsupportedEncodingException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return urlBuilder.toString();
|
||
|
}
|
||
|
|
||
|
public static void main(String[] args) throws IOException {
|
||
|
Map<String,Object> map = getWeatherInfo();
|
||
|
List<Map<String,Object>> datas = (List<Map<String,Object>>)map.get("Datas");
|
||
|
datas.forEach(item -> {
|
||
|
List<Map<String,Object>> weathers = (List<Map<String,Object>>)item.get("Weathers");
|
||
|
if(weathers.size()>0){
|
||
|
PageData info = new PageData();
|
||
|
info.put("METEOROLOGICALINFO_ID", UuidUtil.get32UUID());
|
||
|
info.put("OPERATTIME", item.get("DateTime"));
|
||
|
info.put("CODE", item.get("ID"));
|
||
|
weathers.forEach(weather ->{
|
||
|
if(weather.get("Index").equals(3)){
|
||
|
info.put("TEMPERATURE", weather.get("Value").toString());
|
||
|
}
|
||
|
if(weather.get("Index").equals(6)){
|
||
|
info.put("WINDSPEED", weather.get("Value").toString());
|
||
|
}
|
||
|
if(weather.get("Index").equals(7)){
|
||
|
info.put("WINDDIRECTION", weather.get("Value").toString());
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|