天气预报接口

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @Description: TODO(使用中央天气预报接口【非官方】调用天气)
 * 城市编号参见 http://blog.csdn.net/zhanbiane/article/details/54927340
 * @author zhanbian 
 * @date 2017年2月9日 上午10:00:45
 */
public class Test {
	
	public static void main(String[] args) {
		getWeatherInfo();
	}
	
	//以上海为例
	private static void getWeatherInfo(){
	  StringBuilder info = new StringBuilder();
	  BufferedReader buff = null;
	  
	  URL url;
	try {
		url = new URL("http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101020100&weatherType=0");
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		
		conn.connect();
		if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
			buff = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf8"));
			String str;
			while((str = buff.readLine())!=null){
				info.append(str);
			}
			JSONObject jsonObj = JSON.parseObject(info.toString());
			JSONObject weatherinfo = jsonObj.getJSONObject("weatherinfo");
			
			//日期
			String weatherDate = weatherinfo.getString("date_y");
			
			//地点
			String city = weatherinfo.getString("city");
			
			//温度
			String temp1 = weatherinfo.getString("temp1");//今天
			String temp2 = weatherinfo.getString("temp2");//明天
			
			//天气
			String weather1 = weatherinfo.getString("weather1");//今天
			String weather2 = weatherinfo.getString("weather2");//明天
			
			//风向
			String wind1 = weatherinfo.getString("wind1");//今天
			String wind2 = weatherinfo.getString("wind2");//明天
			
			System.out.println("检测时间:"+weatherDate+"\t城市"+city);
			System.out.println("今天天气状况\t温度:"+temp1+"\t天气:"+weather1+"\t风向:"+wind1);
			System.out.println("明天天气状况\t温度:"+temp2+"\t天气:"+weather2+"\t风向:"+wind2);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}finally{
		if(buff!=null){
			try {
				buff.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	}
}


城市编号参见:http://blog.csdn.net/zhanbiane/article/details/54927340



气象局官方代言网站,智慧天气:http://smart.weather.com.cn

要填写申请信息,审核通过才可,调用需要认证参数。


下面提供几个免费的接口,(参考:http://www.android100.org/html/201507/24/167958.html)

中华万年历:
http://wthrcdn.etouch.cn/weather_mini?city=北京
通过城市名字获得天气数据,json数据
http://wthrcdn.etouch.cn/weather_mini?citykey=101010100
通过城市id获得天气数据,json数据


123.毒霸
http://weather.123.duba.net/static/weather_info/101121301.html


中央天气预报:
http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101040100&weatherType=0


http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101040100&weatherType=1


别的地址:
http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100
通过城市id获得天气数据,xml文件数据,
当错误时会有<error>节点
http://wthrcdn.etouch.cn/WeatherApi?city=北京
通过城市名字获得天气数据,xml文件数据





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值