android 百度天气接口

百度天气接口

之前有篇随笔是说中国气象的天气接口的,但那个接口不是很稳定,用一段时间过后就会获取不到数据了。

随笔地址:http://www.cnblogs.com/liangstudyhome/p/3700420.html

1.最近看到百度有个天气接口,拿来用了用,感觉挺好的,这里就说说这个接口了

百度提供天气预报查询接口API,可以根据经纬度/城市名查询天气情况。

接口说明

根据经纬度/城市名查询天气的结果

接口示例

http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey
百度ak申请地址:http://lbsyun.baidu.com/apiconsole/keyAK申请到以后,就可以直接用这个接口了。

接口参数说明


参数类型 参数名称 是否必须 具体描述
String ak true 开发者密钥
String sn false 若用户所用ak的校验方式为sn校验时该参数必须。 
String location true 输入城市名或经纬度,城市名称如:北京,经纬度格式为lng,lat坐标如: location=116.305145,39.982368;城市天气预报中间"|"分 隔,location=116.305145,39.982368| 122.305145,36.982368|….
String output false 输出的数据格式,默认为xml格式,当output设置为’json’时,输出的为json格式的数据;
String coord_type false 请求参数坐标类型,默认为gcj02经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托坐标,gcj02表示经过国测局加密的坐标。wgs84表示gps获取的坐标。


返回结果


参数名称 含义 说明
currentCity 当前城市 返回城市名
status 返回结果状态信息  
date 当前时间 年-月-日
results 天气预报信息 白天可返回近期3天的天气情况(今天、明天、后天)、晚上可返回近期4天的天气情况(今天、明天、后天、大后天)
results.currentCity 当前城市  
results.weather_data weather_data.date 天气预报时间  
weather_data.dayPictureUrl 白天的天气预报图片url  
weather_data.nightPictureUrl 晚上的天气预报图片url  
weather_data.weather 天气状况 所有天气情况(”|”分隔符):晴|多云|阴|阵雨|雷阵雨|雷阵雨伴有冰雹|雨夹雪|小雨|中雨|大雨|暴雨|大暴雨|特大暴雨|阵雪|小雪| 中雪|大雪|暴雪|雾|冻雨|沙尘暴|小雨转中雨|中雨转大雨|大雨转暴雨|暴雨转大暴雨|大暴雨转特大暴雨|小雪转中雪|中雪转大雪|大雪转暴雪|浮 尘|扬沙|强沙尘暴|霾
weather_data.wind 风力  
weather_data.temperature 温度  
2.在网页上查看数据接口的数据XML格式的数据:
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <CityWeatherResponse>
 3     <status>success</status>
 4     <date>2014-05-03</date>
 5     <results>
 6
 7         <currentCity>北京</currentCity>
 8         <weather_data>
 9                             <date>周六(今天, 实时:22℃)</date>
10                 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/leizhenyu.png</dayPictureUrl>
11                 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>
12                 <weather>雷阵雨转多云</weather>
13                 <wind>北风5-6级</wind>
14                 <temperature>24 ~ 11℃</temperature>
15                             <date>周日</date>
16                 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
17                 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
18                 <weather>多云转晴</weather>
19                 <wind>北风4-5级</wind>
20                 <temperature>19 ~ 8℃</temperature>
21                             <date>周一</date>
22                 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png</dayPictureUrl>
23                 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
24                 <weather>晴</weather>
25                 <wind>微风</wind>
26                 <temperature>21 ~ 9℃</temperature>
27                             <date>周二</date>
28                 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
29                 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
30                 <weather>多云转晴</weather>
31                 <wind>微风</wind>
32                 <temperature>21 ~ 10℃</temperature>
33                     </weather_data>
34         </results>
35 </CityWeatherResponse>

JSON形式的数据:

{
    "error": 0,
    "status": "success",
    "date": "2014-05-03",
    "results": [
        {
            "currentCity": "北京",
            "weather_data": [
                {
                    "date": "周六(今天, 实时:22℃)",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/leizhenyu.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
                    "weather": "雷阵雨转多云",
                    "wind": "北风5-6级",
                    "temperature": "24 ~ 11℃"
                },
                {
                    "date": "周日",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
                    "weather": "多云转晴",
                    "wind": "北风4-5级",
                    "temperature": "19 ~ 8℃"
                },
                {
                    "date": "周一",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
                    "weather": "晴",
                    "wind": "微风",
                    "temperature": "21 ~ 9℃"
                },
                {
                    "date": "周二",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
                    "weather": "多云转晴",
                    "wind": "微风",
                    "temperature": "21 ~ 10℃"
                }
            ]
        }
    ]
}

下面用一个demo来获取天气的数据(以获取json为例 就不解析了):


  手机上获取到的数据:

部分代码:

 1 public class MainActivity extends Activity {
 2
 3     private TextView showTextView;
 4
 5     private AsyncHttpClient mAsyncHttpClient;
 6
 7     private String Url = "你的百度天气接口";
 8
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13
14         showTextView = (TextView) findViewById(R.id.showTextView);
15
16         mAsyncHttpClient = new AsyncHttpClient();
17
18         mAsyncHttpClient.get(Url, null, new JsonHttpResponseHandler() {
19
20             @Override
21             public void onSuccess(JSONObject result) {
22                 super.onSuccess(result);
23
24                 showTextView.setText(result.toString());
25             }
26
27             @Override
28             public void onFinish() {
29                 // TODO Auto-generated method stub
30                 super.onFinish();
31             }
32
33         });
34
35     }
36 }
时间: 2024-08-14 11:08:25

android 百度天气接口的相关文章

Android天气预报+百度天气接口

首先 在准备编写程序之前有几点准备工作 1首先需要调节Android的DNS地址.(这个我会在末尾提及) http://www.eoeandroid.com/forum.php?mod=viewthread&tid=309756&page=1#pid3230430 2Android中的SMS短信的控制.(这个我会在末尾提及) http://blog.csdn.net/xieqibao/article/details/6748716 3 Android的AK码的申请,这个百度AK可以自己申请

如何在android程序中使用百度api接口:

百度地图.百度语音.百度导航.百度定位等等.以下为使用百度天气提供的api,具有天气查询,城市设置,短信分享天气等基本功能,界面清爽,不过现在因为百度key的原因失效了,不能更新天气了.srceduswustiweatherwebUpdateWeather.java中的AK替换成自己申请的百度API KEY,申请地址http://lbsyun.baidu.com/apiconsole/key.代码有比较详细的注释.代码量也不大,有兴趣的朋友可以自己排查一下.项目编码UTF-8 默认编译版本4.2

Android中 GsonFormat插件解析Jason 数据+和风天气接口解析案例

首先 转载自http://www.cnblogs.com/androidsuperman/p/4579249.html     感谢 @西北野狼  同学. 第一部分: GsonFormat 插件基本使用方法 很久以前写json解析用原始的解析json的方法,后来为了加快开发进度,开始使用gson,fastjson等第三方jar包来进行json解析,为了保持apk足够小,不因为引入jar包导致apk文件过大,选择使用gson来加速json解析,当然,去年androidstudio 1.0版本出来,

Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图

标签: Android百度地图API Key  分类: Android 百度地图开发(2)  最近自己想研究下地图,本来想研究google Map,但是申请API key比较坑爹,于是从百度地图入手,其实他们的用法都差不多,本篇文章就带领大家在自己的Android项目中加入百度地图的功能,接下来我会写一系列关于百度地图的文章,欢迎大家到时候关注!   一 申请API key 在使用百度地图之前,我们必须去申请一个百度地图的API key,申请地址http://lbsyun.baidu.com/a

android 百度地图开发实例(转载)

因为在我的寝室google基站定位返回的数据总是为空,所以换成百度地图,发现百度地图开发起来非常方便,提供了许多有用的工具,地图的加载速度也比google地图快许多. 为了加强记忆,写一点android 百度地图开发常用的方法. 1初始化 MapManager mBMapMan = new BMapManager(this);boolean isSuccess = mBMapMan.init(this.mStrKey, new MyGeneralListener()); isSuccess 的值

支持APP手机应用(android和ios)接口调用 ,传输验证可用 shiro 的 MD5、SHA 等加密

请认准本正版代码,售后技术有保障,代码有持续更新.(盗版可耻,违者必究)         此为本公司团队开发 ------------------------------------------------------------------------------------------------------------------------- 1. 有 oracle .msyql.spring3.0.spring4.0  一共 4 套版本全部提供没有打jar没有加密的源代码(最下面截图2

Android百度地图 - 在地图上标注已知GPS纬度经度值的一个或一组覆盖物 - OPEN 开发经验库 - 360安全浏览器 8.1

首页   代码   文档   问答   资讯   经验   GitHub日报 登录   注册 www.open-open.com/libOPEN经验 投稿 全部经验分类  Android IOS JavaScript HTML5 CSS jQuery Python PHP NodeJS Java Spring MySQL MongoDB Redis NOSQL Vim C++ C# JSON Ruby Linux Nginx Docker 所有分类  >  开发语言与工具  >  移动开发  

[android] 百度地图开发 (三).定位当前位置及getLastKnownLocation获取location总为空问题

       前一篇百度地图开发讲述"(二).定位城市位置和城市POI搜索",主要通过监听对象MKSearchListener类实现城市兴趣点POI(Point of Interest)搜索.该篇讲述定位当前自己的位置及使用getLastKnownLocation获取location总时为空值的问题. 一. 定位当前位置的原理及实现       定位当前位置可以通过LBS(Location Based Service,基于位置的服务),主要工作原理是利用无线网络Network或GPS定

[android] 百度地图开发 (一).申请AK显示地图及解决显示空白网格问题

    最近做android百度地图,但是使用baidumapapi_v2_3_1.jar和libBaiduMapSDK_v2_3_1.so显示百度地图时总是遇到问题--只显示网格而没有显示地图,网络连接和APIKey申请都是正确的,就是不知道为什么不能显示,网上也有很多人遇到这个问题,有的是SDK更新不兼容.而且网上很多百度地图都是使用V2.3.1版本,后来没有办法只有通过baidumapapi_v2_4_1.jar完成显示地图. 其中需要注意的是使用2.3.1时调用manager.init(