一、項目演示效果如下:
二、使用 聚合數據SDK:
(1)我使用的是聚合數據上的天氣預報查詢,地址:https://www.juhe.cn/docs/api/id/39(2)註冊賬號—創建一個新應用(在個人中心頁面—數據中心—申請數據)–填入自己的應用–找到分類–天氣預報—全國天氣預報
(3)下載sdk:https://www.juhe.cn/juhesdk/download
(4)參考開發文檔:https://www.juhe.cn/juhesdk/adocs
接下來介紹了聚合數據SDK的初始化和權限的使用!
三、項目介紹
(1)項目截圖(2)WeatherApplication.java這裏作爲sdk的初始化
這裏是初始化聚合數據的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數據,容易出錯!特此標記出來!
(5)WeatherService.java主要是處理後臺服務的類,將數據的加載放在後臺!
注意的是:
這裏的白色部分的數值是在聚合數據上申請的時候的OpenID(在個人中心的中心可以找到OpenID!)