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

今天做了一个微信公众平台的小功能,用到了天气预报功能 ,所以就写了一个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是北京的编码,具体的可以看我写的另一篇文章来具体了解这个内容。

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

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

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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值