引入pom依赖
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.3</version> </dependency>
创建表
CREATE TABLE `weather` (
`id` varchar(36) NOT NULL,
`city_name` varchar(32) DEFAULT NULL COMMENT '城市名称',
`city` varchar(32) DEFAULT NULL COMMENT '城市编码',
`temp` int(11) DEFAULT NULL COMMENT '温度',
`wd` varchar(32) DEFAULT NULL COMMENT '风向',
`ws` varchar(32) DEFAULT NULL COMMENT '风力',
`wse` varchar(32) DEFAULT NULL COMMENT '风速',
`sd` varchar(32) DEFAULT NULL COMMENT '湿度',
`date_time` datetime DEFAULT NULL COMMENT '日期时间',
`weather` varchar(32) DEFAULT NULL COMMENT '天气',
`qy` varchar(32) DEFAULT NULL COMMENT '气压',
`njd` varchar(32) DEFAULT NULL COMMENT '能见度',
`rain` decimal(10,2) DEFAULT NULL COMMENT '降雨量',
`rain24h` decimal(10,2) DEFAULT NULL COMMENT '降雨量24小时',
`aqi` int(11) DEFAULT NULL COMMENT '空气质量',
`limitnumber` varchar(32) DEFAULT NULL COMMENT '限号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
启动类添加 @EnableScheduling 注解开启定时任务
通过定时任务每天获取并存到本地数据库
@Scheduled(cron = "0 0 8 * * ?") public void getWeather() throws Exception{ OkHttpClient client = new OkHttpClient().newBuilder() .build(); Request request = new Request.Builder() .url("http://d1.weather.com.cn/sk_2d/101050101.html") .method("GET", null) .addHeader("Referer", "http://www.weather.com.cn/") .build(); Response response = client.newCall(request).execute(); String body = response.body().string(); //去除多余字符串 body = body.substring(body.indexOf('{')); JSONObject jsonObject = JSONObject.parseObject(body); //创建实体类 Weather weather = new Weather(); weather.setId(RandomUtils.nextLong() + ""); weather.setCityName(jsonObject.getString("cityname")); weather.setCity(jsonObject.getString("city")); weather.setTemp(jsonObject.getInteger("temp").longValue()); weather.setWd(jsonObject.getString("WD")); weather.setWs(jsonObject.getString("WS")); weather.setWse(jsonObject.getString("wse")); weather.setSd(jsonObject.getString("SD")); weather.setQy(jsonObject.getString("qy")); weather.setNjd(jsonObject.getString("njd")); weather.setDateTime(dateFormat(jsonObject.getString("date"),jsonObject.getString("time"))); weather.setRain(jsonObject.getBigDecimal("rain")); weather.setRain24h(jsonObject.getBigDecimal("rain24h")); weather.setAqi(jsonObject.getInteger("aqi").longValue()); weather.setWeather(jsonObject.getString("weather")); weatherService.insertWeather(weather); } /** * 日期格式转换 * [url=home.php?mod=space&uid=952169]@Param[/url] dateStr 日期 * @param timeStr 时间 * @return */ private Date dateFormat(String dateStr, String timeStr) { try { Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); String dateString = year + "年" + dateStr.substring(0, dateStr.indexOf("(")) + timeStr; DateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); Date date; date = format.parse(dateString); return date; } catch (ParseException e) { throw new RuntimeException(e); } }