素材巴巴 > 程序开发 >

用百度天气接口的实现安卓天气预报(json数据)

程序开发 2023-09-03 18:57:49

代码地址:

http://download.csdn.net/detail/u012606241/7608515

 

 

具体思路:

1.写一个线程,其中city_str就是城市名,例如

 

city_str="宁波";


 

2.然后通过线程取百度天气接口json数据

 

网址是:http://api.map.baidu.com/telematics/v3/weather?location=宁波&output=json&ak=GuZriL3rkm1MUnyTyfsNGvTC

(注意不要用ie开这个网址,用谷歌、火狐那些应该都可以,我用的是火狐)

打开网址后,就可以看到那些json数据了。

 

3.对取到数据进行处理(在void doingData(String data)函数里进行处理),并根据这些数据设置自己的控件显示

 

线程代码如下:

 

 

class MyAsyncTask extends AsyncTask {@Overrideprotected void onPreExecute() {// TODO Auto-generated method stub// super.onPreExecute();}}@Overrideprotected String doInBackground(String... params) {// TODO Auto-generated method stubString path = "";try {city_str = cityname.getText().toString();citys_strlength=city_str.length();city_str = java.net.URLEncoder.encode(city_str, "UTF-8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}path = "http://api.map.baidu.com/telematics/v3/weather?location="+ city_str + "&output=json&ak=GuZriL3rkm1MUnyTyfsNGvTC";// http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=GuZriL3rkm1MUnyTyfsNGvTCString jsonString = HttpUtils.getJsonContent(path);return jsonString;// 作为result传递到onPostExecute}@Overrideprotected void onPostExecute(String result) {// TODO Auto-generated method stub// super.onPostExecute(result);doingData(result);// 处理得到的数据}}public void doingData(String data) {System.out.println("百度天气接口数据在这"+data);
 //在这里处理数据,并根据所得到的值设置控件的显示就ok了。	
 }


 

4.加入代码后,在onCreate(Bundle savedInstanceState)函数中加入以下这句代码就ok了。

 

new MyAsyncTask().execute();


 

 结果数据部分:

 

在doingData(String data)部分将得到的数据打印出来,就可以在system.out.print中看到这样的数据:

 

{"error":0,"status":"success","date":"2014-05-12","results":[{"currentCity":"u5b81u6ce2","weather_data":[{"date":"u5468u4e00(u4ecau5929, u5b9eu65f6uff1a19u2103)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"u591au4e91","wind":"u5faeu98ce","temperature":"15u2103"},{"date":"u5468u4e8c","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/zhenyu.png","weather":"u9634u8f6cu9635u96e8","wind":"u5faeu98ce","temperature":"26 ~ 19u2103"},{"date":"u5468u4e09","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/zhenyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/zhenyu.png","weather":"u9635u96e8","wind":"u5faeu98ce","temperature":"25 ~ 20u2103"},{"date":"u5468u56db","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"u591au4e91","wind":"u5faeu98ce","temperature":"26 ~ 15u2103"}]}]
 }


 


 

将json数据拿去   【百度  json数据解析】  一下就可以看到这样的结果(可以根据解析结果进行数据分析):

 

 

 


 

 资源地址:http://download.csdn.net/detail/u012606241/7608515    (0积分的)


 

 

 


标签:

上一篇: Nginx 中部署 Angular 项目遇到的坑 下一篇:
素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。