JAVA调用国家气象局的天气预报(返回json格式)

来源:互联网 发布:mac safari打不开网页 编辑:程序博客网 时间:2024/06/06 10:40

今天做了一个微信公众平台的小功能,用到了天气预报功能 ,所以就写了一个demo供大家参考,水平一般般,如此不对之处或代码欠优之处,请大家帮忙指正。话不多说了 直接上代码,看不懂的童鞋可以留言,或者左边的QQ联系我的哦。

 

注:由于返回格式是json的,所以这里用到了json解析,所以你的项目中需要导入json的jar包,大家可以在网上搜一下,这里就不指出了。

我用的是这一个:json应用jar包

下面是一个测试的demo的所有代码:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import net.sf.json.JSONObject;public class TestWeather {public static void main(String[] args) {URL url=null;String inputline="";String info="";try {url=new URL("http://m.weather.com.cn/data/101010100.html");HttpURLConnection conn=(HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");InputStreamReader inReader=new InputStreamReader(conn.getInputStream(),"utf-8");BufferedReader bufferedReader=new BufferedReader(inReader);while((inputline=bufferedReader.readLine())!=null){info+=inputline;}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}JSONObject jsonob=JSONObject.fromObject(JSONObject.fromObject(info).getString("weatherinfo"));String city=jsonob.getString("city");//城市 String day=jsonob.getString("date_y");//日期String week=jsonob.getString("week");//星期String temp1=jsonob.getString("temp1");//第1天String temp2=jsonob.getString("temp2");//第2天String temp3=jsonob.getString("temp3");//第3天String temp4=jsonob.getString("temp4");//第4天String temp5=jsonob.getString("temp5");//第5天String temp6=jsonob.getString("temp6");//第6天String tempF1=jsonob.getString("tempF1");//第1天String tempF2=jsonob.getString("tempF2");//第2天String tempF3=jsonob.getString("tempF3");//第3天String tempF4=jsonob.getString("tempF4");//第4天String tempF5=jsonob.getString("tempF5");//第5天String tempF6=jsonob.getString("tempF6");//第6天String weather1=jsonob.getString("weather1");//第1天天气String weather2=jsonob.getString("weather2");//第2天天气String weather3=jsonob.getString("weather3");//第3天天气String weather4=jsonob.getString("weather4");//第4天天气String weather5=jsonob.getString("weather5");//第5天天气String weather6=jsonob.getString("weather6");//第6天天气String wind1=jsonob.getString("wind1");//第1天风速String wind2=jsonob.getString("wind2");//第2天风速String wind3=jsonob.getString("wind3");//第3天风速String wind4=jsonob.getString("wind4");//第4天风速String wind5=jsonob.getString("wind5");//第5天风速String wind6=jsonob.getString("wind6");//第6天风速String index=jsonob.getString("index");//今天穿衣指数String index_d=jsonob.getString("index_d");//今天穿衣指数String index48=jsonob.getString("index48");//48小时穿衣指数String index48_d=jsonob.getString("index48_d");//48小时穿衣指数String index_uv=jsonob.getString("index_uv");//紫外线及48小时紫外线String index48_uv=jsonob.getString("index48_uv");//紫外线及48小时紫外线String index_xc=jsonob.getString("index_xc");//洗车String index_tr=jsonob.getString("index_tr");//旅游String index_co=jsonob.getString("index_co");//舒适指数String index_cl=jsonob.getString("index_cl");//晨练String index_ls=jsonob.getString("index_tr");//晾晒String index_ag=jsonob.getString("index_ag");//过敏System.out.println("城市:"+city+"\t\t时间:"+day+"\t\t星期:"+week);System.out.println("\t---为您预报今后6天的天气情况---");System.out.println("第1天天气:"+weather1+"\t风速:"+wind1+"\t摄氏温度:"+temp1+"\t华氏温度"+tempF1);System.out.println("第2天天气:"+weather2+"\t风速:"+wind2+"\t摄氏温度:"+temp2+"\t华氏温度"+tempF2);System.out.println("第3天天气:"+weather3+"\t风速:"+wind3+"\t摄氏温度:"+temp3+"\t华氏温度"+tempF3);System.out.println("第4天天气:"+weather4+"\t风速:"+wind4+"\t摄氏温度:"+temp4+"\t华氏温度"+tempF4);System.out.println("第5天天气:"+weather5+"\t风速:"+wind5+"\t摄氏温度:"+temp5+"\t华氏温度"+tempF5);System.out.println("第6天天气:"+weather6+"\t风速:"+wind6+"\t摄氏温度:"+temp6+"\t华氏温度"+tempF6);System.out.println("今天的穿衣指数为:"+index+"\t"+index_d);System.out.println("48小时穿衣指数为:"+index48+"\t"+index48_d);System.out.println("紫外线及48小时紫外线指数为:"+index_uv+"\t"+index48_uv);System.out.println("洗车:"+index_xc);System.out.println("旅游:"+index_tr);System.out.println("舒适指数:"+index_co);System.out.println("晨练:"+index_cl);System.out.println("晾晒:"+index_ls);System.out.println("过敏:"+index_ag);}}


上面的代码中这一句:http://m.weather.com.cn/data/101010100.html  这个是国家天气网的网址,后面的101010100是北京的编码,具体的可以看我写的另一篇文章来具体了解这个内容。

在这里贴上另一篇文章,有需要的可以参考一下:

中国国家气象局天气预报信息接口 .

贴上我上图代码的结果,有图有真相。