項目源代碼下載地址:java
http://yunpan.cn/cZVWIieuFyK9I 訪問密碼 2eacandroid
(1)聚合數據官網地址:http://www.juhe.cn/json
(2)註冊帳號—建立一個新應用(在我的中心頁面—數據中心—申請數據)–填入本身的應用–找到分類–天氣預報—全國天氣預報api
(3)下載sdk (因爲項目使用的是1點幾的版本,因此請下載:包含在個人項目中!)app
(4)參考開發文檔:http://www.juhe.cn/juhesdk/adocside
這裏介紹了聚合數據SDK的初始化和權限的使用!spa
(1)項目截圖code
(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!)