根据IP地址获取所在城市的天气情况(Java)
王杨_weikisa
回复
Trafalgar_qing: [code=java]
@GetMapping("/getWeather")
public String getWeather(HttpServletRequest request,Model model){
//获取真实IP地址
String ipAddress =IpUtil.getIpAddr(request);
System.out.println(ipAddress);
if(ipAddress.equals("0:0:0:0:0:0:0:1")) {
// 1.192.119.149 鄭州
// 223.104.212.171 上海
// 42.227.160.255 南陽
ipAddress = "223.104.212.171";//如果都失败,则默认设置为上海的一个IP地址
}
Weather weather = apiutil.returnWeather(ipAddress);
if(weather!=null&&weather.getCity().contains("未知")) {//今日接口次数使用已达上线
model.addAttribute("weathers",weather);
model.addAttribute("aqi",weather.getAqi());
model.addAttribute("city",weather.getCity());
model.addAttribute("message","今日请求次数已达上限");
}else if(weather!=null&&weather.getCity().contains("获取失败")) {//HttpGetPost请求超时(Time Out)
model.addAttribute("weathers",weather);
model.addAttribute("aqi",weath
[/code]
|