java 获取温度_html获取实时天气&Java获取天气

对接天气的api很多都有次数限制,或者需要去申请一些appcode才能访问,有没有一些比较简单的方式来获取实时天气呢?基于此,本人整理了此篇博客。

一、天气对接。

中国天气网

1、 http://m.weather.com.cn/data/101110101.html 已失效

2、 http://www.weather.com.cn/data/sk/101010100.html 有数据,乱码,不全,没有生活指数,没有天气图标

{

"weatherinfo": {

"city": "北京",

"cityid": "101010100",

"temp": "22",

"WD": "北风",

"WS": "2级",

"SD": "19%",

"WSE": "2",

"time": "16:05",

"isRadar": "1",

"Radar": "JC_RADAR_AZ9010_JB"

}

}

3、 http://www.weather.com.cn/data/cityinfo/101010100.html 有数据,乱码,不全,没有生活指数,没有天气图标

{

"weatherinfo": {

"city": "北京",

"cityid": "101010100",

"temp1": "24℃",

"temp2": "11℃",

"weather": "雷阵雨转多云",

"img1": "d4.gif",

"img2": "n1.gif",

"ptime": "11:00"

}

}

4、 http://www.weather.com.cn/data/zs/101010100.html 有数据,乱码,信息比较全

中华万年历:

5、 http://wthrcdn.etouch.cn/weather_mini?city=北京 通过城市名字获得天气数据,json数据

6、 http://wthrcdn.etouch.cn/weather_mini?citykey=101010100 通过城市id获得天气数据,json数据

7、 http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100 通过城市id获得天气数据,xml文件数据,当错误时会有节点

8、 http://wthrcdn.etouch.cn/WeatherApi?city=北京 通过城市名字获得天气数据,xml文件数据

Github

9、 https://github.com/jokermonn/-Api/blob/master/CenterWeather.md 获取中国天气预报,但获取的不是实时的数据

10、 http://api.k780.com:88/?app=weather.history&weaid=hefei&date=2018-06-22&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json 获取实时天气

11、 http://api.k780.com:88/?app=weather.future&weaid=1&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json 获取未来几天

中央天气预报:

12、 http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101040100&weatherType=0 已失效

13、 http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101040100&weatherType=1 已失效

收费_聚合数据

14、 天气预报:https://www.juhe.cn/docs/api/id/73

15、 全国天气预报:https://www.juhe.cn/docs/api/id/39

其他:

16、小米:http://weatherapi.market.xiaomi.com/wtr-v2/weather?cityId=101121301 有数据、乱码

17、新浪天气预报API:http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0 已失效

友情链接:

1、天气API接口大全(nohacks.cn 收集整理):http://www.nohacks.cn/post-35.html

2、网上的天气 API 哪一个更加可靠?:https://www.zhihu.com/question/20575288

3、免费的天气接口(满足你的大部分需求):http://blog.csdn.net/wanghao940101/article/details/72123184

4、真正的中国天气api接口xml,json(求加精) ...:http://blog.csdn.net/fancylovejava/article/details/26102635

5、免费的天气接口(满足你的大部分需求):http://blog.csdn.net/wanghao940101/article/details/72123184

6、通过城市名(北京/北京市)获取当前城市当前温度及未来一周天气预报 (Json数据) :包含台湾的主要县市接口:

http://wthrcdn.etouch.cn/weather_mini?city=北京市;

https://www.sojson.com/open/api/weather/json.shtml?city=北京

https://www.sojson.com/open/api/weather/xml.shtml?city=北京

7、http://i.tianqi.com/index.php?c=code&id=12&icon=1&py=hefei&num=5

二、运用

我采用的是中华万年历的api,( http://wthrcdn.etouch.cn/WeatherApi?city=北京 通过城市名字获得天气数据,xml文件数据),此api调用的底层也是中国天气网的数据,跟百度上直接搜索的天气保持一致,此api的缺点是返回的xml数据没有天气的图标,需要自己做一些匹配,这个也比较简单。

demo:

$(document).ready(function () {

var city = getQueryString("city");

var params = new Object();

params.city=city;

sendData("http://wthrcdn.etouch.cn/WeatherApi", params, function(data){

var str=XML2String(data);

//创建文档对象

var parser=new DOMParser();

var xmlDoc=parser.parseFromString(str,"text/xml");

//城市

var city = xmlDoc.getElementsByTagName('city');

$("#city").html(city[0].textContent)

var fengli = xmlDoc.getElementsByTagName('fengli');

var shidu = xmlDoc.getElementsByTagName('shidu');

$("#fengli").html("风力"+fengli[1].textContent+" 湿度"+shidu[0].textContent);

//高温

var high = xmlDoc.getElementsByTagName('high');

//低温

var low = xmlDoc.getElementsByTagName('low');

//天气

var type = xmlDoc.getElementsByTagName('type');

$("#tianqi").html(type[0].textContent)

});

})

2、Java获取天气

思路:调用接口获取数据,简历实体类,将获取的xml数据转换成实体类,需要一些jar包的支持

5b2db65a2aca3c6411bd83cd5e03892a.png

packagecom.cn;/*** 天气实体类

*@authoryunjuanyunshuxue

**/

public classWeatherInfo {private String date;//时间

private String cityname;//城市名

private String weather;//天气

private String temperature;//气温

private String airquality;//pm2.5

publicString getDate() {returndate;

}public voidsetDate(String date) {this.date =date;

}publicString getCityname() {returncityname;

}public voidsetCityname(String cityname) {this.cityname =cityname;

}publicString getWeather() {returnweather;

}public voidsetWeather(String weather) {this.weather =weather;

}publicString getTemperature() {returntemperature;

}public voidsetTemperature(String temperature) {this.temperature =temperature;

}publicString getAirquality() {returnairquality;

}public voidsetAirquality(String airquality) {this.airquality =airquality;

}

@OverridepublicString toString() {return "WeatherInfo [date=" + date + ", cityname=" +cityname+ ", weather=" + weather + ", temperature=" +temperature+ ", airquality=" + airquality + "]";

}

}

packagecom.cn;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.UnsupportedEncodingException;importjava.net.MalformedURLException;importjava.net.URL;importjava.net.URLConnection;importjava.net.URLEncoder;importjava.util.zip.GZIPInputStream;importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;importorg.junit.Test;/*** 通过get请求向网站http://wthrcdn.etouch.cn/weather_mini获取某个 城市的天气状况数据,数据格式是Json

*

*@authoryunjuanyunshuxue

**/

public classWeatherUtils {/*** 通过城市名称获取该城市的天气信息

*

*@paramcityName

*@return

*/

public staticString GetWeatherData(String cityname) {

StringBuilder sb=newStringBuilder();try{

String weather_url= "http://wthrcdn.etouch.cn/weather_mini?city="+cityname;

URL url= newURL(weather_url);

URLConnection conn=url.openConnection();

InputStream is=conn.getInputStream();

GZIPInputStream gzin= newGZIPInputStream(is);

InputStreamReader isr= new InputStreamReader(gzin, "utf-8"); //设置读取流的编码格式,自定义编码

BufferedReader reader = newBufferedReader(isr);

String line= null;while((line=reader.readLine())!=null)

sb.append(line+" ");

reader.close();

}catch(UnsupportedEncodingException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(MalformedURLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}//System.out.println(sb.toString());

returnsb.toString();

}/*** 将JSON格式数据进行解析 ,返回一个weather对象

*@paramstr

*@return

*/

public staticWeatherInfo GetWeather(String weatherInfobyJson){

JSONObject dataOfJson=JSONObject.fromObject(weatherInfobyJson);if(dataOfJson.getInt("status")!=1000)return null;//创建WeatherInfo对象,提取所需的天气信息

WeatherInfo weatherInfo = newWeatherInfo();//从json数据中提取数据

String data = dataOfJson.getString("data");

dataOfJson=JSONObject.fromObject(data);

weatherInfo.setCityname(dataOfJson.getString("city"));;

weatherInfo.setAirquality(dataOfJson.getString("aqi"));//获取预测的天气预报信息

JSONArray forecast = dataOfJson.getJSONArray("forecast");//取得当天的

JSONObject result=forecast.getJSONObject(0);

weatherInfo.setDate(result.getString("date"));

String high= result.getString("high").substring(2);

String low= result.getString("low").substring(2);

weatherInfo.setTemperature(low+"~"+high);

weatherInfo.setWeather(result.getString("type"));returnweatherInfo;

}

}

packagecom.cn;public classTest {public static voidmain(String[] args){

String info= WeatherUtils.GetWeatherData("北京");

WeatherInfo weatherinfo=WeatherUtils.GetWeather(info);

System.out.println(weatherinfo.toString());

}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值