后端根据地名获取最近天气情况
话不多说直接上代码:
public class Test {
public static void main(String[] args) {
String s = exp1(“奉化”);
System.out.println(s);
}
static String exp1(String cityName){
StringBuilder sb=new StringBuilder();
try {
URL url=new URL(“http://wthrcdn.etouch.cn/weather_mini?city=”+cityName);
URLConnection urlConnection=url.openConnection();
InputStream is=urlConnection.getInputStream();
GZIPInputStream gzin = new GZIPInputStream(is);
InputStreamReader isr=new InputStreamReader(gzin,“utf-8”);
BufferedReader br=new BufferedReader(isr);
String str=null;
while ((str=br.readLine())!=null){
sb.append(str);
}
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
输出结果
{“data”:{“yesterday”:{“date”:“3日星期二”,“high”:“高温 19℃”,“fx”:“北风”,“low”:“低温 11℃”,“fl”:"<![CDATA[3级]]>",“type”:“晴”},“city”:“奉化”,“forecast”:[{“date”:“4日星期三”,“high”:“高温 19℃”,“fengli”:"<![CDATA[2级]]>",“low”:“低温 13℃”,“fengxiang”:“北风”,“type”:“多云”},{“date”:“5日星期四”,“high”:“高温 21℃”,“fengli”:"<![CDATA[1级]]>",“low”:“低温 14℃”,“fengxiang”:“东南风”,“type”:“阴”},{“date”:“6日星期五”,“high”:“高温 25℃”,“fengli”:"<![CDATA[2级]]>",“low”:“低温 13℃”,“fengxiang”:“北风”,“type”:“晴”},{“date”:“7日星期六”,“high”:“高温 22℃”,“fengli”:"<![CDATA[2级]]>",“low”:“低温 13℃”,“fengxiang”:“北风”,“type”:“多云”},{“date”:“8日星期天”,“high”:“高温 19℃”,“fengli”:"<![CDATA[3级]]>",“low”:“低温 11℃”,“fengxiang”:“北风”,“type”:“晴”}],“ganmao”:“感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。”,“wendu”:“18”},“status”:1000,“desc”:“OK”}