天气Api http://apicloud.mob.com/v1/weather/ip?key=270c4d225bcf0&ip=“ip”
更换ip
后台获取ip的方法:
request.getRemoteAddr();
前台获取ip的方法:
java后台访问网站的方法:
public static String SendGET(){
String result=””;//访问返回结果
BufferedReader read=null;//读取访问结果
try {
//创建url
URL realurl=new URL("http://apicloud.mob.com/v1/weather/ip?key=270c4d225bcf0&ip=121.123.92.228");
//打开连接
URLConnection connection=realurl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//建立连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段,获取到cookies等
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
read = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;//循环读取
while ((line = read.readLine()) != null) {
result += line;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(read!=null){//关闭流
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
返回的json串:
{“msg”:“success”,
“result”:[{“airCondition”:“轻度污染”,
“airQuality”:{“aqi”:115,
“city”:“北京”,
“district”:“北京”,
“fetureData”:[{“aqi”:88,“date”:“2019-06-20”,“quality”:“良”},
{“aqi”:112,“date”:“2019-06-21”,“quality”:“轻度污染”},
{“aqi”:97,“date”:“2019-06-22”,“quality”:“良”},
{“aqi”:106,“date”:“2019-06-23”,“quality”:“轻度污染”},
{“aqi”:102,“date”:“2019-06-24”,“quality”:“轻度污染”},
{“aqi”:107,“date”:“2019-06-25”,“quality”:“轻度污染”}],
“hourData”:[{“aqi”:115,“dateTime”:“2019-06-19 16:00:00”},
{“aqi”:118,“dateTime”:“2019-06-19 15:00:00”},
{“aqi”:118,“dateTime”:“2019-06-19 14:00:00”},
{“aqi”:117,“dateTime”:“2019-06-19 13:00:00”},
{“aqi”:114,“dateTime”:“2019-06-19 12:00:00”},
{“aqi”:118,“dateTime”:“2019-06-19 11:00:00”},
{“aqi”:117,“dateTime”:“2019-06-19 10:00:00”},
{“aqi”:113,“dateTime”:“2019-06-19 09:00:00”},
{“aqi”:114,“dateTime”:“2019-06-19 08:00:00”},
{“aqi”:113,“dateTime”:“2019-06-19 07:00:00”},
{“aqi”:113,“dateTime”:“2019-06-19 06:00:00”},
{“aqi”:114,“dateTime”:“2019-06-19 05:00:00”},
{“aqi”:108,“dateTime”:“2019-06-19 04:00:00”},
{“aqi”:102,“dateTime”:“2019-06-19 03:00:00”},
{“aqi”:99,“dateTime”:“2019-06-19 02:00:00”},
{“aqi”:89,“dateTime”:“2019-06-19 01:00:00”},
{“aqi”:80,“dateTime”:“2019-06-19 00:00:00”},
{“aqi”:82,“dateTime”:“2019-06-18 23:00:00”},
{“aqi”:72,“dateTime”:“2019-06-18 22:00:00”},
{“aqi”:73,“dateTime”:“2019-06-18 21:00:00”},
{“aqi”:106,“dateTime”:“2019-06-18 20:00:00”},
{“aqi”:120,“dateTime”:“2019-06-18 19:00:00”},
{“aqi”:131,“dateTime”:“2019-06-18 18:00:00”},
{“aqi”:136,“dateTime”:“2019-06-18 17:00:00”}],
“no2”:15,
“pm10”:74,
“pm25”:58,
“province”:“北京”,
“quality”:“轻度污染”,
“so2”:4,
“updateTime”:“2019-06-19 17:00:00”},
“city”:“北京”,
“coldIndex”:””,
“date”:“2019-06-19”,
“distrct”:“北京”,
“dressingIndex”:””,
“exerciseIndex”:””,
“future”:[{“date”:“2019-06-19”,“night”:“阴天”,“temperature”:“22°C”,“week”:“今天”,“wind”:””},
{“date”:“2019-06-20”,“dayTime”:“局部多云”,“night”:“局部多云”,“temperature”:“37°C / 23°C”,“week”:“星期四”,“wind”:“东南偏南风 2级”},
{“date”:“2019-06-21”,“dayTime”:“局部多云”,“night”:“局部多云”,“temperature”:“33°C / 20°C”,“week”:“星期五”,“wind”:“东南偏东风 3级”},
{“date”:“2019-06-22”,“dayTime”:“少云”,“night”:“局部多云”,“temperature”:“34°C / 21°C”,“week”:“星期六”,“wind”:“西南偏南风 2级”},
{“date”:“2019-06-23”,“dayTime”:“局部多云”,“night”:“局部多云”,“temperature”:“36°C / 23°C”,“week”:“星期日”,“wind”:“西南偏南风 3级”},
{“date”:“2019-06-24”,“dayTime”:“阴天”,“night”:“多云”,“temperature”:“36°C / 23°C”,“week”:“星期一”,“wind”:“西南偏南风 3级”},
{“date”:“2019-06-25”,“dayTime”:“阴天”,“night”:“局部多云”,“temperature”:“34°C / 22°C”,“week”:“星期二”,“wind”:“南风 3级”},
{“date”:“2019-06-26”,“dayTime”:“局部多云”,“night”:“局部多云”,“temperature”:“33°C / 21°C”,“week”:“星期三”,“wind”:“东南风 3级”},
{“date”:“2019-06-27”,“dayTime”:“阴天”,“night”:“局部多云”,“temperature”:“33°C / 22°C”,“week”:“星期四”,“wind”:“东南风 2级”},
{“date”:“2019-06-28”,“dayTime”:“局部多云”,“night”:“局部多云”,“temperature”:“34°C / 22°C”,“week”:“星期五”,“wind”:“西北偏北风 3级”}],
“humidity”:“湿度:52%”,
“pollutionIndex”:“115”,
“province”:“北京”,
“sunrise”:“04:45”,
“sunset”:“19:44”,
“temperature”:“30℃”,
“time”:“17:00”,
“updateTime”:“20190619171256”,
“washIndex”:””,
“weather”:“多云”,
“week”:“周三”,
“wind”:“西南风2级”}],
“retCode”:“200”}