获取天气信息作者是通过高德开放平台来实现的。
1、在该平台注册个账号
如已经注册,直接登录即可。
2、创建应用
创建成功后获取到key值。(注意该值会经常用到,且可以共用保存好)
3、进入开发文档选择天气查询
下面按照说明使用即可。
接下来上代码。(注意功能接受的是JSON格式的)
4、pom.xml文件的引用
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.80</version>
</dependency>
5、配置文件
无
6、实体类
package com.zjh.weixin.Model;
import lombok.Data;
import java.util.List;
/**
* @Author: zjh
* @Date: 2022/9/19 11:03
* @Description: 天气总实体
* @Version: v1.0
*/
@Data
public class WeatherModel {
private String status;
private String count;
private String nfo;
private String infocode;
private List<LivesModel> lives;
}
该实体为总实体(后续添加功能时,在总实体添加引用,在创建子实体)
package com.zjh.weixin.Model;
import lombok.Data;
/**
* @Author zjh
* @Date 2022/9/19 1053
* @Description 天气实体类
* @Version v1.0
*/
@Data
public class LivesModel {
private String province;
private String city;
private String adcode;
private String weather ;
private String temperature ;
private String winddirection;
private String windpower;
private String humidity;
private String reporttime ;
}
该类为子实体
7、根据路径获取信息
package com.zjh.weixin.Util;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import java.util.Map;
/**
* @Author: zjh
* @Date: 2022/9/19 10:29
* @Description: 后端根据路径发起请求
* @Version: v1.0
*/
public class HttpsUtil {
/**
* get请求
*/
public static String get(String url){
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(mediaType);
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class);
return result;
}
}
8、调用方法
public static void main(String[] args) {
String url = "https://restapi.amap.com/v3/weather/weatherInfo?city=411400&key=b2f301************077d21";
String s = HttpsUtil.get(url);
System.out.println(s);
JSONObject object = JSONObject.parseObject(s);
WeatherModel jb = (WeatherModel)JSONObject.toJavaObject(object, WeatherModel.class);
System.out.println(jb);
LivesModel livesModel = jb.getLives().get(0);
System.out.println(livesModel);
int c =Integer.valueOf(livesModel.getTemperature());
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
String currSun = dateFm.format(new Date());
String r = null;
if (c<=26 && c>=18){
r="今天温度适宜";
}else if (c>26){
r="今天温度较高,注意防晒";
}else if(c<18){
r="今天温度较低,注意保暖";
}
String zong = "日期: "+livesModel.getReporttime()+" "+currSun+"\n"+
"所在城市: "+livesModel.getCity()+"\n"+
"天气: " + livesModel.getWeather()+"\n"+
"温度: "+ livesModel.getTemperature()+" "+r+"\n"+
"风向: "+ livesModel.getWinddirection()+"风"+"\n"+
"风级: "+livesModel.getWindpower();
System.out.println(zong);
}
成功返回数据
9、项目优化
9.1、高德开放平台根据地址获取城市编码
public static String getWeather(String adcode,String key){
String url = "https://restapi.amap.com/v3/weather/weatherInfo?city="+adcode+"&key="+key;
String s = HttpsUtil.get(url);
JSONObject object = JSONObject.parseObject(s);
WeatherModel jb = (WeatherModel)JSONObject.toJavaObject(object, WeatherModel.class);
LivesModel livesModel = jb.getLives().get(0);
int c =Integer.valueOf(livesModel.getTemperature());
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
String currSun = dateFm.format(new Date());
String r = null;
if (c<=26 && c>=18){
r="今天温度适宜";
}else if (c>26){
r="今天温度较高,注意防晒";
}else if(c<18){
r="今天温度较低,注意保暖";
}
String zong = "日期: "+livesModel.getReporttime()+" "+currSun+"\n"+
"所在城市: "+livesModel.getCity()+"\n"+
"天气: " + livesModel.getWeather()+"\n"+
"温度: "+ livesModel.getTemperature()+" "+r+"\n"+
"风向: "+ livesModel.getWinddirection()+"风"+"\n"+
"风级: "+livesModel.getWindpower();
return zong;
}
private static String getCityCode(String adress,String key){
String url = "https://restapi.amap.com/v3/geocode/geo?address="+adress+"&key="+key;
String s = HttpsUtil.get(url);
JSONObject object = JSONObject.parseObject(s);
System.out.println(object);
JSONArray array = (JSONArray) object.get("geocodes");
JSONObject arrayObject = (JSONObject) array.get(0);
String adcode = arrayObject.get("adcode").toString();
return adcode;
}
public static void main(String[] args) {
System.out.println(getCityCode("河南省郑州市","b2f301c36******77d21"));
System.out.println(getWeather(getCityCode("河南省郑州市","b2f3*****7d21"),"b2f3*******7d21"));
}
注:该功能虽说是springboot项目,但是缺失许多东西,不太完善(其主要原因是因为作者发现通过java编程实现通过用户账号发送给该用户的好友信息,只能通过Robot的自动测试功能实现微信发送消息。该方法缺点太大,不太方便,暂时未找到其他更好的方法(如有其他方法望留言告知),只为做测试(说实话就是为了玩),所以项目不太完善,后续会会不定时更新的。)
如用那些方法不太好,望指教(感激不尽)。
(该功能作者会不定时的更新补充,防止丢失请关注作者博客。)