您现在的位置是: 首页 > 天气预报 天气预报

聊城新浪天气预报_新浪天气预报api

tamoadmin 2024-08-14 人已围观

简介1.天气预报API2.各个天气预报APP数据是怎样接入的,都是统一的来源吗?百度API Key申请地址:://lbsyun.baidu/apiconsole/key创建应用 如图:?提交后得到API Key 如图:?接口实例:://api.map.baidu/telematics/v3/weather?location=南昌&output=json&ak=你的API Key&mcode=你的数字签

1.天气预报API

2.各个天气预报APP数据是怎样接入的,都是统一的来源吗?

聊城新浪天气预报_新浪天气预报api

百度API Key申请地址:://lbsyun.baidu/apiconsole/key

创建应用 如图:?

提交后得到API Key 如图:?

接口实例:://api.map.baidu/telematics/v3/weather?location=南昌&output=json&ak=你的API Key&mcode=你的数字签名SHA1;com.example.administrator.jsontest(包名)

接口参数说明

参数类型

参数名称

是否必须

具体描述

String location true 输入城市名或经纬度,城市名如北京或者131,经纬度格式为lng,lat坐标,如:location=116.305145,39.982368;全国值为all,返回省会城市自治区,港澳台天气情况多城市天气预报中间用“|”分隔,如:location=116.305145,39.982368|123.342323,36238945|...

String output false 输出的数据格式,默认为xml格式,当output设置为json时,输出的为json数据格式

String coord_type false 请求参数坐标类型,默认为gcj02经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84;

返回的JSON数据

{

"error":0,

"status":"success",

"date":"2016-03-05",

"results":[

{

"currentCity":"北京",

"pm25":"144",

"index":[

{

"title":"穿衣",

"zs":"较冷",

"tipt":"穿衣指数",

"des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},

{

"title":"洗车",

"zs":"不宜",

"tipt":"洗车指数",

"des":"不宜洗车,未来24小时内有扬沙或浮尘,如果在此期间洗车,极易很快蒙上新的灰尘。"},

{

"title":"旅游",

"zs":"一般",

"tipt":"旅游指数",

"des":"风稍大,扬沙或浮尘天气对能见度和空气质量都会有些影响,出行请注意交通安全和取适当的防尘措施。"},

{

"title":"感冒",

"zs":"易发",

"tipt":"感冒指数",

"des":"昼夜温差大,风力较强,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"},

{

"title":"运动",

"zs":"较不宜",

"tipt":"运动指数",

"des":"有扬沙或浮尘,建议适当停止户外运动,选择在室内进行运动,以避免吸入更多沙尘,有损健康。"},

{

"title":"紫外线强度",

"zs":"最弱",

"tipt":"紫外线强度指数",

"des":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}

? ],

?"weather_data":[

?{

?"date":"周六 03月05日 (实时:12℃)", ?"dayPictureUrl":"://api.map.baidu/images/weather/day/fuchen.png",

"nightPictureUrl":"://api.map.baidu/images/weather/night/qing.png",

?"weather":"浮尘转晴",

?"wind":"北风4-5级",

?"temperature":"12 ~ -1℃"},

?{

?"date":"周日",

"dayPictureUrl":"://api.map.baidu/images/weather/day/duoyun.png",

"nightPictureUrl":"://api.map.baidu/images/weather/night/duoyun.png",

?"weather":"多云",

?"wind":"微风",

?"temperature":"10 ~ -3℃"},

?{

?"date":"周一", "dayPictureUrl":"://api.map.baidu/images/weather/day/duoyun.png",

"nightPictureUrl":"://api.map.baidu/images/weather/night/yin.png",

?"weather":"多云转阴",

?"wind":"微风",

?"temperature":"13 ~ 2℃"},

?{

?"date":"周二", "dayPictureUrl":"://api.map.baidu/images/weather/day/yin.png",

"nightPictureUrl":"://api.map.baidu/images/weather/night/duoyun.png",

?"weather":"阴转多云",

?"wind":"北风3-4级",

?"temperature":"6 ~ -1℃"}

?]}]}

3. ?我们来写个demo,代码如下:

package com.example.administrator.jsontest;

public class MainActivity extends Activity {

private Button button;

private TextView textView;

private Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case 0:

String re = (String) msg.obj;

textView.setText(re);

break;

}

}

};

@Override

protected void onCreate(Bundle sedInstanceState) {

super.onCreate(sedInstanceState);

setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.button);

textView = (TextView) findViewById(R.id.textView);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Log.i("T", "点击了Button");

sendRequestWithHttpClient();

}

});

}

private void sendRequestWithHttpClient() {

new Thread(new Runnable() {

@Override

public void run() {

HttpURLConnection connection = null;

try {

URL url = new URL("://api.map.baidu/telematics/v3/weather?location=南昌&output=json&ak=8ixCCFzlBB617YX7tONI2P5B&mcode=1C:6B:42:33:E8:A6:DC:A2:11:6E:26:EC:84:BD:42:E3:8E:6B:57:9A;com.example.administrator.jsontest");

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

connection.setConnectTimeout(5000);

connection.setReadTimeout(5000);

InputStream in = connection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

StringBuilder response = new StringBuilder();

String line;

while ((line = reader.readLine()) != null) {

response.end(line);

}

Log.i("T", response.toString()); parseJSONObjectOrJSONArray(response.toString());

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}).start();

}

//解析JSON数据

private void parseJSONObjectOrJSONArray(String jsonData) {

try {

String count = "";

JSONObject jsonObject = new JSONObject(jsonData);

JSONArray jsonArray = jsonObject.getJSONArray("results");

if (jsonArray.length() > 0) {

JSONObject object = jsonArray.getJSONObject(0);

String city = object.optString("currentCity");

JSONArray array = object.getJSONArray("weather_data");

for (int i = 0; i < array.length(); i++) {

JSONObject jsonObject1 = array.getJSONObject(i);

String dateDay = jsonObject1.optString("date");

String weather = jsonObject1.optString("weather");

String wind = jsonObject1.optString("wind");

String temperature = jsonObject1.optString("temperature");

count =count +"\n"+ dateDay + " " + weather + " " + wind + " " + temperature;

Log.i("AAA",count);

}

Message message = new Message();

message.what = 0;

message.obj = count;

handler.sendMessage(message);

}

} catch (JSONException e) {

e.printStackTrace();

}

}

}

4.?运行结果如下:?

天气预报API

中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。

关于获取天气预报的实例参考android学习手册,里面有源码。android学习手册包含9个章节,108个例子,源码文档随便看,例子都是可交互,可运行,源码用android studio目录结构,高亮显示代码,文档都用文档结构图显示,可以快速定位。360手机助手中下载,图标上有贝壳

中国国家气象局天气预报接口总共提供了三个:

://.weather.cn/data/sk/101010100.html

://.weather.cn/data/cityinfo/101010100.html

://m.weather.cn/data/101010100.html

注:上面接口2014.3.4日已不再更新。换成这个:://m.weather.cn/atad/101230201.html。各位看34楼。在此感谢tdwll和黄晓佳cobish。

最详细的信息来自第三个接口。上面url中的101010100是城市代码,这里是北京的城市代码。只需要改变城市代码,就可以得到所在城市的天气信息。笔者在福州,所以选择的城市代码是福州101230101。

在浏览器上输入url:://m.weather.cn/data/101230101.html得到信息,天气信息是json的数据格式,数据如下:

{"weatherinfo":{"city":"福州","city_en":"fuzhou","date_y":"2012年5月14日","date":"","week":"星期一","fchh":"08","cityid":"101230101","temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃","tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉","weather1":"阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨转阵雨","weather6":"阵雨转小雨","img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3","img_title1":"阵雨","img_title2":"中雨","img_title3":"中雨","img_title4":"小雨","img_title5":"小雨","img_title6":"小雨","img_title7":"小雨","img_title8":"小雨","img_title9":"小雨","img_title10":"阵雨","img_title11":"阵雨","img_title12":"小雨","img_title_single":"阵雨","wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"热","index_d":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单裤。","index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。","index_uv":"弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适宜","index_co":"较不舒适","st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18","index_cl":"较不宜","index_ls":"不太适宜","index_ag":"不易发"}}

我们可以解析json数据去得到自己想用的天气信息。

天气信息解释:

[html] view plain copy print?

{?

"weatherinfo":{?

<!--?基本信息?-->?

"city":"福州",?

"city_en":"fuzhou",?

"date_y":"2012年5月14日",?

"date":"",?

"week":"星期一",?

"fchh":"08",?

"cityid":"101230101",?

<!--?从今天开始到第六天的每天的天气情况,这里的温度是摄氏温度?-->?

"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃",?

<!--?从今天开始到第六天的每天的天气情况,这里的温度是华氏温度?-->?

"tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",?

<!--?天气描述?-->?

"weather1":"阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨转阵雨","weather6":"阵雨转小雨",?

<!--?天气描述序号?-->?

"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3",?

<!--?名称?-->?

"img_title1":"阵雨","img_title2":"中雨","img_title3":"中雨","img_title4":"小雨","img_title5":"小雨","img_title6":"小雨","img_title7":"小雨","img_title8":"小雨","img_title9":"小雨","img_title10":"阵雨","img_title11":"阵雨","img_title12":"小雨","img_title_single":"阵雨",?

<!--?风速描述?-->?

"wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风",?

<!--?风速级别描述?-->?

"fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",?

<!--?今天穿衣指数?-->?

"index":"热",?

"index_d":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单裤。",?

<!--?48小时穿衣指数?-->?

"index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",?

<!--?紫外线及48小时紫外线?-->?

"index_uv":"弱","index48_uv":"最弱",?

<!--?洗车?-->?

"index_xc":"不宜",?

<!--?旅游?-->?

"index_tr":"适宜",、?

<!--?舒适指数?-->?

"index_co":"较不舒适",?

"st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",?

<!--?晨练?-->?

"index_cl":"较不宜",?

<!--?晾晒?-->?

"index_ls":"不太适宜",?

<!--?过敏?-->?

"index_ag":"不易发"?

}?

}?

{

"weatherinfo":{

<!-- 基本信息 -->

"city":"福州",

"city_en":"fuzhou",

"date_y":"2012年5月14日",

"date":"",

"week":"星期一",

"fchh":"08",

"cityid":"101230101",

<!-- 从今天开始到第六天的每天的天气情况,这里的温度是摄氏温度 -->

"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃",

<!-- 从今天开始到第六天的每天的天气情况,这里的温度是华氏温度 -->

"tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",

<!-- 天气描述 -->

"weather1":"阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨转阵雨","weather6":"阵雨转小雨",

<!-- 天气描述序号 -->

"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3",

<!-- 名称 -->

"img_title1":"阵雨","img_title2":"中雨","img_title3":"中雨","img_title4":"小雨","img_title5":"小雨","img_title6":"小雨","img_title7":"小雨","img_title8":"小雨","img_title9":"小雨","img_title10":"阵雨","img_title11":"阵雨","img_title12":"小雨","img_title_single":"阵雨",

<!-- 风速描述 -->

"wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风",

<!-- 风速级别描述 -->

"fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",

<!-- 今天穿衣指数 -->

"index":"热",

"index_d":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单裤。",

<!-- 48小时穿衣指数 -->

"index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",

<!-- 紫外线及48小时紫外线 -->

"index_uv":"弱","index48_uv":"最弱",

<!-- 洗车 -->

"index_xc":"不宜",

<!-- 旅游 -->

"index_tr":"适宜",、

<!-- 舒适指数 -->

"index_co":"较不舒适",

"st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",

<!-- 晨练 -->

"index_cl":"较不宜",

<!-- 晾晒 -->

"index_ls":"不太适宜",

<!-- 过敏 -->

"index_ag":"不易发"

}

}

各个天气预报APP数据是怎样接入的,都是统一的来源吗?

://.codeproject/KB/gadgets/WeatherSideShow.aspx

这是codeproject上的一个天气预报桌面应用程序。

可以在://.codeproject/搜索weather关键字,有很多软件。都是开源的。

另外还可以去://sourceforge.net/搜索weather,结果肯定更多。也都是开源的。

这两个站点汇聚了太多人的智慧结晶,肯定有你要的。

首先可以明确一点,所有的气象数据一定都是来自官方的,以非官方名义公布天气数据应该还没有人敢,只是国家天气数据也有不准的时候,所有的公司都面临一个问题,怎么样让自己的数据比竞品更准?1、国家天气数据为什么也会不准?

国家天气数据中的一部分是地面、高空、雷达观测数据,会自动汇总到国家气象信息中心通信台。

一部分是卫星数据,比如中国的风云卫星一般需要20多分钟才能完成全球扫描,所以这部分的数据通常有半小时左右的延迟。

最后是预报数据,气象学上有一些标准模型,跑模型的时间,数据打包传输分发的时间其实是比较长的(由于数据量的关系),比如气象局可能用欧洲中心模式系统的数据,有一些延迟,但是预报的特性就是随着时间不断接近,预测越是准确,很容易理解。就我所知,国家高校的一些研究机构也有自己的模型,也不敢说哪个就一定是最准的。

(一部分信息来自中国气象网)

2、国家天气数据可能是不全面的

有些时候我们想要通过手机传感器获得的经纬度来获取天气数据,或者通过?IP?地址来获取天气数据,这些都是很常见的需求,而国家天气数据的?API?可能不支持。

或者,地方气象台有一些地方的气象数据,它们属于省级气象数据,并不需要上报到国家级,那么自然国家天气数据的?API?不支持,但是地方依旧会公布它们。

亦或者,很多人关心 PM2.5,这些数据不是来自气象局,而是来自环保局,而且环保局还没有空气质量预报,又要从其他机构接入。