一、項目演示效果如下:
項目源代碼下載地址:
http://yunpan.cn/cZVWIieuFyK9I 訪問密碼 2eac
二分别、使用 聚合數(shù)據(jù)SDK:
(1)聚合數(shù)據(jù)官網(wǎng)地址:https://www.juhe.cn/
(2)注冊賬號—創(chuàng)建一個新應(yīng)用(在個人中心頁面—數(shù)據(jù)中心—申請數(shù)據(jù))–填入自己的應(yīng)用–找到分類–天氣預(yù)報—全國天氣預(yù)報
(3)下載sdk (由于項目使用的是1點幾的版本恨锚,所以請下載:包含在我的項目中!)
(4)參考開發(fā)文檔:https://www.juhe.cn/juhesdk/adocs
這里介紹了聚合數(shù)據(jù)SDK的初始化和權(quán)限的使用!
x三、項目介紹**
(1)項目截圖
(2)WeatherApplication.java這里作為sdk的初始化
<pre>package com.juhe.weather;import com.thinkland.juheapi.common.CommonFun;import android.app.Application;/**
- 這里是初始化聚合數(shù)據(jù)的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());
}
}</pre>
我們需要在清單文件中加入:
<pre>android:name="com.juhe.weather.WeatherApplication"</pre>
(3)返回的json數(shù)據(jù)格式
<pre>{
"resultcode": "200",
"reason": "successed!",
"result": {
"sk": {
"temp": "19",
"wind_direction": "西北風(fēng)",
"wind_strength": "1級",
"humidity": "37%",
"time": "16:00"
},
"today": {
"temperature": "5℃~19℃",
"weather": "多云轉(zhuǎn)陰",
"weather_id": {
"fa": "01",
"fb": "02"
},
"wind": "南風(fēng)微風(fēng)",
"week": "星期五",
"city": "成都",
"date_y": "2015年03月13日",
"dressing_index": "較舒適",
"dressing_advice": "建議著薄外套信殊、開衫牛仔衫褲等服裝。年老體弱者應(yīng)適當(dāng)添加衣物汁果,宜著夾克衫涡拘、薄毛衣等。",
"uv_index": "最弱",
"comfort_index": "",
"wash_index": "較適宜",
"travel_index": "",
"exercise_index": "較不宜",
"drying_index": ""
},
"future": {
"day_20150313": {
"temperature": "5℃~19℃",
"weather": "多云轉(zhuǎn)陰",
"weather_id": {
"fa": "01",
"fb": "02"
},
"wind": "南風(fēng)微風(fēng)",
"week": "星期五",
"date": "20150313"
},
"day_20150319": {
"temperature": "10℃~18℃",
"weather": "陰",
"weather_id": {
"fa": "02",
"fb": "02"
},
"wind": "南風(fēng)微風(fēng)",
"week": "星期四",
"date": "20150319"
}
}
},
"error_code": 0
}
未來三天的json數(shù)據(jù)格式:
{
"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
}</pre>
(4)根據(jù)上述的json格式须鼎,我們需要創(chuàng)建相應(yīng)的json試題對象鲸伴,其中為了更好的處理府蔗,分別建立相應(yīng)的類
上圖中就是含有包含關(guān)系的json數(shù)據(jù)晋控,容易出錯!特此標(biāo)記出來姓赤!
(5)WeatherService.java主要是處理后臺服務(wù)的類赡译,將數(shù)據(jù)的加載放在后臺!
注意的是:
這里的白色部分的數(shù)值是在聚合數(shù)據(jù)上申請的時候的OpenID(在個人中心的中心首頁就可以找到OpenID2幻)