项目源代码下载地址:
http://yunpan.cn/cZVWIieuFyK9I 访问密码 2eac
二、使用 聚合数据SDK:
(1)聚合数据官网地址:http://www.juhe.cn/
(2)注册账号—创建一个新应用(在个人中心页面—数据中心—申请数据)–填入自己的应用–找到分类–天气预报—全国天气预报
(3)下载sdk (由于项目使用的是1点几的版本,所以请下载:包含在我的项目中!)
(4)参考开发文档:http://www.juhe.cn/juhesdk/adocs
这里介绍了聚合数据SDK的初始化和权限的使用!
三、项目介绍
(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"
{
"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数据,容易出错!特此标记出来!
(5)WeatherService.java主要是处理后台服务的类,将数据的加载放在后台!
这里的白色部分的数值是在聚合数据上申请的时候的OpenID(在个人中心的中心首页就可以找到OpenID!)