Android程序之全国天气预报查询(聚合数据开发)
一、项目演示效果例如以下:
项目源码下载地址:
http://pan.baidu.com/s/1pL6o5Mb password:5myq
二、使用 聚合数据SDK:
(1)聚合数据官网地址:http://www.juhe.cn/
(2)注冊账号—创建一个新应用(在个人中心页面—数据中心—申请数据)–填入自己的应用–找到分类–天气预报—全国天气预报
(3)下载sdk (因为项目使用的是1点几的版本号,所以请下载:包括在我的项目中!)
(4)參考开发文档:http://www.juhe.cn/juhesdk/adocs
这里介绍了聚合数据SDK的初始化和权限的使用!
三、项目介绍
(1)项目截图
(2)WeatherApplication.java这里作为sdk的初始化
package com.juhe.weather;
import com.thinkland.juheapi.common.CommonFun;
import android.app.Application;
/**
* 这里是初始化聚合数据的SDK
*
* 须要在application中加入 android:name="com.juhe.weather.WeatherApplication
*
* @author xuliugen
*
*/
public class WeatherApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 使用聚合SDK须要初始化的过程
CommonFun.initialize(getApplicationContext());
}
}
我们须要在清单文件里加入:
android:name="com.juhe.weather.WeatherApplication"
(3)返回的json数据格式
{
"resultcode": "200",
"reason": "successed!",
"result": {
"sk": {
"temp": "19",
"wind_direction": "西北风",
"wind_strength": "1级",
"humidity": "37%",
"time": "16:00"
},
"today": {
"temperature": "5℃~19℃",
"weather": "多云转阴",
"weather_id": {
"fa": "01",
"fb": "02"
},
"wind": "南风微风",
"week": "星期五",
"city": "成都",
"date_y": "2015年03月13日",
"dressing_index": "较舒适",
"dressing_advice": "建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当加入衣物。宜着夹克衫、薄毛衣等。
",
"uv_index": "最弱",
"comfort_index": "",
"wash_index": "较适宜",
"travel_index": "",
"exercise_index": "较不宜",
"drying_index": ""
},
"future": {
"day_20150313": {
"temperature": "5℃~19℃",
"weather": "多云转阴",
"weather_id": {
"fa": "01",
"fb": "02"
},
"wind": "南风微风",
"week": "星期五",
"date": "20150313"
},
"day_20150319": {
"temperature": "10℃~18℃",
"weather": "阴",
"weather_id": {
"fa": "02",
"fb": "02"
},
"wind": "南风微风",
"week": "星期四",
"date": "20150319"
}
}
},
"error_code": 0
}
未来三天的json数据格式:
{
"resultcode": "200",
"reason": "successed!",
"result": [{
"weatherid": "01",
"weather": "多云",
"temp1": "16",
"temp2": "13",
"sh": "17",
"eh": "20",
"date": "20150313",
"sfdate": "20150313170000",
"efdate": "20150313200000"
},
{
"weatherid": "02",
"weather": "阴",
"temp1": "19",
"temp2": "16",
"sh": "11",
"eh": "17",
"date": "20150320",
"sfdate": "20150320110000",
"efdate": "20150320170000"
}],
"error_code": 0
}
(4)依据上述的json格式。我们须要创建对应的json试题对象,当中为了更好的处理。分别建立对应的类
上图中就是含有包括关系的json数据,easy出错!
特此标记出来!
(5)WeatherService.java主要是处理后台服务的类,将数据的载入放在后台!
注意的是:
这里的白色部分的数值是在聚合数据上申请的时候的OpenID(在个人中心的中心首页就能够找到OpenID。)