Android程式之全國天氣預報查詢(聚合資料開發)
阿新 • • 發佈:2019-02-11
一、專案演示效果如下:
(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資料,容易出錯!特此標記出來!
(5)WeatherService.java主要是處理後臺服務的類,將資料的載入放在後臺!
注意的是:
這裡的白色部分的數值是在聚合資料上申請的時候的OpenID(在個人中心的中心首頁就可以找到OpenID!)