整合百度天气API

由于自己闲着的时候写了一个微信公众平台的小应用 也参考了很多别人的源码 再此感谢那些被我参考过的大神们

本着开源的精神 http://git.oschina.net/bddiudiu/wechat_Spring 我的项目也开源了 大部分都是参考了 智云同学的项目 

项目目前部署与新浪SEA  访问地址  http://diuwx.sinaapp.com


好了 不多说微信的这个项目 我们来讲一下 如何整合百度的API一起进来 顺便也给自己日后留下一些痕迹


首先我们查看百度车联网的API页面 我们找到我们需要的天气接口 同时我们也申请一个ak key 具体申请KEY这里就不说了

得到KEY 以及 api的url和各种参数 我们开始将百度的天气接口整合进我们的项目


百度的这个接口,我们要在项目中模拟一次http提交.

既然这样 那我们就看一下他的url提交地址

?
1
http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey

看一下地址我们就会看出来,这个url需要拼接3个参数.

location 我们需要查询的城市

output 返回给我们的数据格式

ak 我们的 ak key

当然他还有别的参数 我们这里暂时不用.


好 知道了url之后我们在项目中新建一个工具类 我们就叫他baiduutil.java 

然后我们新建3个参数

?
1
2
3
private  static  String api =  "http://api.map.baidu.com/telematics/v3/weather?" ;
private  static  String output =  "json" ;
private  static  String ak =  "your key" ;

然后 我们写一个方法 就是向百度提交url 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public  static  String getSend(String str){
                 //将传进来的城市转码
         try  {
             str = URLEncoder.encode(str,  "utf-8" );
         catch  (Exception e) {
             e.printStackTrace();
         }
         //拼接url
         StringBuffer buf =  new  StringBuffer();
         buf.append( "location=" );
         buf.append(str);
         buf.append( "&output=" );
         buf.append(output);
         buf.append( "&ak=" );
         buf.append(ak);
         String param = buf.toString();
                 //这是接收百度API返回的内容
         String result =  "" ;
         String urlName = api + param;
         URL realUrl;
         try  {
             realUrl =  new  URL(urlName);
             // 打开和URL之间的连接
             URLConnection conn = realUrl.openConnection();
             conn.setDoInput( true );
             BufferedReader br =  new  BufferedReader( new  InputStreamReader(conn.getInputStream()));
             
             String line = br.readLine();
             while  (line !=  null ) {
                 result += line;
                 line = br.readLine();
             }
             br.close();
             return  result;
         catch  (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return  null ;
         
     }

以上的代码将会返回来一段json数据.当然如果你的output是xml你得到的就是xml格式的.

下面我们就需要解析这返回来的json数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{
     "error" : 0 ,
     "status" : "success" ,
     "date" : "2014-05-09" ,
     "results" :[
         {
             "currentCity" : "\u5317\u4eac" ,
             "weather_data" :[
                 {
                     "date" : "\u5468\u4e94(\u4eca\u5929, \u5b9e\u65f6\uff1a19\u2103)" ,
                     "dayPictureUrl" : "http:\/\/api.map.baidu.com\/images\/weather\/day\/yin.png" ,
                     "nightPictureUrl" : "http:\/\/api.map.baidu.com\/images\/weather\/night\/yin.png" ,
                     "weather" : "\u9634" ,
                     "wind" : "\u5fae\u98ce" ,
                     "temperature" : "20 ~ 14\u2103"
                 },
                 {
                     "date" : "\u5468\u516d" ,
                     "dayPictureUrl" : "http:\/\/api.map.baidu.com\/images\/weather\/day\/zhenyu.png" ,
                     "nightPictureUrl" : "http:\/\/api.map.baidu.com\/images\/weather\/night\/xiaoyu.png" ,
                     "weather" : "\u9635\u96e8\u8f6c\u5c0f\u96e8" ,
                     "wind" : "\u5fae\u98ce" ,
                     "temperature" : "19 ~ 11\u2103"
                 },
                 {
                     "date" : "\u5468\u65e5" ,
                     "dayPictureUrl" : "http:\/\/api.map.baidu.com\/images\/weather\/day\/xiaoyu.png" ,
                     "nightPictureUrl" : "http:\/\/api.map.baidu.com\/images\/weather\/night\/qing.png" ,
                     "weather" : "\u5c0f\u96e8\u8f6c\u6674" ,
                     "wind" : "\u5fae\u98ce" ,
                     "temperature" : "19 ~ 10\u2103"
                 },
                 {
                     "date" : "\u5468\u4e00" ,
                     "dayPictureUrl" : "http:\/\/api.map.baidu.com\/images\/weather\/day\/qing.png" ,
                     "nightPictureUrl" : "http:\/\/api.map.baidu.com\/images\/weather\/night\/qing.png" ,
                     "weather" : "\u6674" ,
                     "wind" : "\u5fae\u98ce" ,
                     "temperature" : "28 ~ 14\u2103"
                 }
             ]
         }
     ]
}

以上就是百度返回给我们的json格式.

我们使用Gson来解析.

首先新建3个类  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class  Status{
     private  String error;
     private  String status;
     private  String date;
     private  List<Results> results;
}
 
class  Results{
     private  String currentCity;
     private  List<Weather> weather_data;
}
 
class  Weather{
     private  String date;
     private  String dayPictureUrl;
     private  String nightPictureUrl;
     private  String weather;
     private  String wind;
     private  String temperature;
}

并生成get/set方法.

然后 我们使用Gson来解析 传进来的JSON字符串是 json

?
1
2
Gson gson =  new  Gson();
Status status = gson.fromJson(json, Status. class );

然后下面就是自己去获取 status里面的内容 然后返回到 微信公众平台

from:http://my.oschina.net/bddiudiu/blog/262636


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值