1、获取请求的ip地址(高德地图api)
package com.yami.shop.common.util; import lombok.extern.slf4j.Slf4j; import javax.servlet.http.HttpServletRequest; import java.net.*; import java.net.InetAddress; @Slf4j public class IpInfoUtil { public static String getIpAddr(HttpServletRequest request) throws SocketException { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } if(ip != null) { ip=ip.split(",")[0]; } return ip; /*String ip = "http://pv.sohu.com/cityjson?ie=utf-8"; String inputLine = ""; String read = ""; String toIp = ""; try { URL url = new URL(ip); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); while ((read = in.readLine()) != null) { inputLine += read; } String ObjJson = inputLine.substring(inputLine.indexOf("=") + 1, inputLine.length() - 1); JSONObject jsonObj = JSON.parseObject(ObjJson); toIp = jsonObj.getString("cip"); } catch (Exception e) { toIp = ""; } return toIp;*/ } }
2、获取高德地图的城市code
@Override public String getAdcode(String ipAddr) { String request = ipUrl + "key=" + key + "&ip=" + ipAddr + "&output=JSON"; //String request = ipUrl + "key=" + key + "&output=JSON"; String res = HttpUtil.get(request); JSONObject jsonObject = JSONObject.parseObject(res); String status = jsonObject.getString("status"); if (!"1".equals(status)) throw new OtherException("获取ip方式错误!"); String infocode = jsonObject.getString("infocode"); if (!"10000".equals(infocode)) throw new OtherException("获取ip返回转态失败!"); return jsonObject.getString("adcode"); }
3、根据code获取天气
@Override public WeatherDTO getWeather(String city) { String request = url + city + "&key=" + key; String res = HttpUtil.get(request); JSONObject jsonObject = JSONObject.parseObject(res); String lives = jsonObject.getString("lives"); Object o = JSONObject.parseArray(lives).get(0); JSONObject weather = JSONObject.parseObject(o.toString()); WeatherDTO weatherDTO = new WeatherDTO(); weatherDTO.setProvince(weather.getString("province")); weatherDTO.setCity(weather.getString("city")); weatherDTO.setWindpower(weather.getString("windpower")); weatherDTO.setWeather(weather.getString("weather")); weatherDTO.setTemperature(weather.getString("temperature")); weatherDTO.setHumidity(weather.getString("humidity")); weatherDTO.setWinddirection(weather.getString("winddirection")); return weatherDTO;
4、DTO
@Data public class WeatherDTO implements Serializable { @ApiModelProperty(value = "省份") private String province; @ApiModelProperty(value = "城市") private String city; @ApiModelProperty(value = "天气现象") private String weather; @ApiModelProperty(value = "实时气温,单位:摄氏度") private String temperature; @ApiModelProperty(value = "风向描述") private String winddirection; @ApiModelProperty(value = "风力级别,单位:级") private String windpower; @ApiModelProperty(value = "空气湿度") private String humidity; @ApiModelProperty(value = "数据发布的时间") private String reporttime; }
5、获取天气
@GetMapping("/weather") @ApiOperation(value = "获取天气", notes = "根据city获取天气预报", response = WeatherDTO.class) public CommonResult getWeather(HttpServletRequest request) throws SocketException { String ipAddr = IpInfoUtil.getIpAddr(request); log.info("获取用户ip地址====================>,{}", ipAddr); //ipAddr = "120.36.246.78"; String adcode = farmAreaService.getAdcode(ipAddr); WeatherDTO weather = farmAreaService.getWeather(adcode); return CommonResult.success(weather); }
标签:String,getString,api,ip,request,weatherDTO,weather,高德 From: https://www.cnblogs.com/ki16/p/16838133.html