Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> WeatherForecast之實現天氣預報

WeatherForecast之實現天氣預報

編輯:關於Android編程

最近心情比較浮躁,項目初步已經完成一直沒有心情來更新博客,基本功能已經實現了包括添加城市,刪除城市,獲取城市部分天氣預報信息,已經詳細的天氣預報信息,還集成了ShareSDK反饋功能,但是體驗上並不怎麼好,後面慢慢優化吧。
獲取天氣預報的信息是一次偶然的機會在一個大神的博客中發現的請求地址,在這裡分享給大家http://weatherapi.market.xiaomi.com/wtr-v2/weather?cityId=101010100&imei=529e2dd3d767bdd3595eec30dd481050&device=pisces&miuiVersion=JXCCNBD20.0&modDevice=&source=miuiWeatherApp,在測試的時候盡量吧imei修改下,擔心人家服務器看到同一個imei請求的次數多了,直接給拉黑,哈哈!
這篇文章主要實現,下面這兩張圖所示的效果:
這裡寫圖片描述

這裡寫圖片描述
首先定義一個接口來獲取天氣預報的相關信息

public interface IGetWeatherData {
    void getWeatherData(String imei,Listlist, GetWeatherDataListener listener);
}

獲取天氣信息成功和失敗的回調

public interface GetWeatherDataListener {
    void onGetWeatherDataSuccess(RawWeatherData rawWeatherData);

    void onGetWeatherDataError(int status);
}

然後在WeatherDataImp 實現IGetWeatherData ,並把數據信息傳給Activity

public class WeatherDataImp implements IGetWeatherData {
    private static WeatherDataImp instance;
    private static Gson gson;
    private String headUrl = "http://weatherapi.market.xiaomi.com/wtr-v2/weather?";
    private String imeiUrl = "&imei=";
    private String footUrl = "&device=pisces&miuiVersion" +
            "=JXCCNBD20.0&modDevice=&source=miuiWeatherApp";
    private static final int ONERROR = 1;

    public static WeatherDataImp getInstance() {
        if (instance == null) {
            synchronized (WeatherDataImp.class) {
                if (instance == null) {
                    instance = new WeatherDataImp();
                    gson = new Gson();
                }
            }
        }
        return instance;
    }

    @Override
    public void getWeatherData(String imei, final List list, final GetWeatherDataListener listener) {
        for (int i = 0; i < list.size(); i++) {
            OkHttpClientManager.getInstance().getEnqueue(headUrl + "cityId=" + list.get(i).cityID + imeiUrl + imei + footUrl, new ResultCallback() {
                @Override
                public void onError(Call call, Exception e) {
                    listener.onGetWeatherDataError(ONERROR);
                }

                @Override
                public void onResponse(Response response) {
                    if (response.code() == 200) {
                        try {
                            listener.onGetWeatherDataSuccess(gson.fromJson(response.body().string(), RawWeatherData.class));
                            Log.d("CESHI",response.body().string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    } else {
                        listener.onGetWeatherDataError(response.code());
                    }

                }
            });
        }
        ;
    }

    public void getDbCityInfo(Context context, final GetDBCityInfoListener listener) {
        final DBManger dbManger = new DBManger(context);
        new Thread(new Runnable() {
            @Override
            public void run() {
                String[] columns = new String[]{"name", "city_id"};
                List cityInfoList = dbManger.query(columns, null, null);
                listener.onGetDBCityInfoSuccess(cityInfoList);
            }
        }).start();
    }
}

到這裡天氣預報的相關信息已經能全部拿到了
json數據示例如下:

{
"forecast":{
"city":"上海",
"city_en":"shanghai",
"cityid":"101020100",
"date":"",
"date_y":"2016年06月06日",
"fchh":"11",
"fl1":"小於3級",
"fl2":"小於3級",
"fl3":"小於3級",
"fl4":"小於3級",
"fl5":"小於3級",
"fl6":"微風",
"fx1":"東南風",
"fx2":"東南風轉南風",
"img1":"",
"img10":"",
"img11":"",
"img12":"",
"img2":"",
"img3":"",
"img4":"",
"img5":"",
"img6":"",
"img7":"",
"img8":"",
"img9":"",
"img_single":"",
"img_title1":"多雲",
"img_title10":"陣雨",
"img_title11":"晴",
"img_title12":"晴",
"img_title2":"雷陣雨",
"img_title3":"陣雨",
"img_title4":"陰",
"img_title5":"陣雨",
"img_title6":"陰",
"img_title7":"陰",
"img_title8":"陣雨",
"img_title9":"陣雨",
"img_title_single":"",
"index":"舒適",
"index48":"",
"index48_d":"",
"index48_uv":"",
"index_ag":"極不易發",
"index_cl":"較適宜",
"index_co":"較舒適",
"index_d":"",
"index_ls":"適宜",
"index_tr":"適宜",
"index_uv":"弱",
"index_xc":"不宜",
"st1":"",
"st2":"",
"st3":"",
"st4":"",
"st5":"",
"st6":"",
"temp1":"27℃~20℃",
"temp2":"27℃~21℃",
"temp3":"26℃~21℃",
"temp4":"28℃~22℃",
"temp5":"26℃~23℃",
"temp6":"0℃~0℃",
"tempF1":"",
"tempF2":"",
"tempF3":"",
"tempF4":"",
"tempF5":"",
"tempF6":"",
"weather1":"多雲轉雷陣雨",
"weather2":"陣雨轉陰",
"weather3":"陣雨轉陰",
"weather4":"陰轉陣雨",
"weather5":"陣雨",
"weather6":"晴",
"week":"星期二",
"wind1":"東南風",
"wind2":"東南風轉南風",
"wind3":"東北風",
"wind4":"東南風",
"wind5":"南風轉東南風",
"wind6":"微風"
},
"realtime":{
"SD":"73%",
"WD":"東北風",
"WS":"1級",
"WSE":"",
"city":"",
"cityid":"101020100",
"isRadar":"1",
"radar":"JC_RADAR_AZ9010_JB",
"temp":"25",
"time":"15:59",
"weather":"多雲"
},
"alert":[
],
"aqi":{
"city":"上海",
"city_id":"101020100",
"pub_time":"2016-06-06 15:00",
"aqi":"68",
"pm25":"47",
"pm10":"66",
"so2":"10",
"no2":"28",
"src":"中國環境監測總站",
"spot":""
},
"index":[
{
"code":"fs",
"details":"紫外線強度較弱,建議塗擦SPF在12-15之間,PA+的防曬護膚品。",
"index":"較弱",
"name":"防曬指數",
"otherName":""
},
{
"code":"ct",
"details":"建議著長袖T恤、襯衫加單褲等服裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。",
"index":"舒適",
"name":"穿衣指數",
"otherName":""
},
{
"code":"yd",
"details":"天氣較好,戶外運動請注意防曬。推薦您進行室內運動。",
"index":"較適宜",
"name":"運動指數",
"otherName":""
},
{
"code":"xc",
"details":"不宜洗車,未來24小時內有雨,如果在此期間洗車,雨水和路上的泥水可能會再次弄髒您的愛車。",
"index":"不宜",
"name":"洗車指數",
"otherName":""
},
{
"code":"ls",
"details":"天氣不錯,適宜晾曬。趕緊把久未見陽光的衣物搬出來吸收一下太陽的味道吧!",
"index":"適宜",
"name":"晾曬指數",
"otherName":""
}
],
"accu_cc":{
"EpochTime":"1465199280",
"LocalObservationDateTime":"2016-06-06T15:48:00+08:00",
"Pressure":"1008.0",
"RealFeelTemperature":"30.9",
"RelativeHumidity":"69",
"UVIndex":"5",
"Visibility":"8.0",
"WindDirectionDegrees":"45",
"WindSpeed":"1.6"
},
"accu_f5":{
"EffectiveEpochDate":"1465232400",
"EffectiveDate":"2016-06-07T01:00:00+08:00",
"DailyForecasts":[
{
"Date":"2016-06-06T07:00:00+08:00",
"EpochDate":"1465167600",
"Sun_Rise":"2016-06-06T04:50:00+08:00",
"Sun_EpochRise":"1465159800",
"Sun_Set":"2016-06-06T18:56:00+08:00",
"Sun_EpochSet":"1465210560",
"PrecipitationProbability":"7"
},
{
"Date":"2016-06-07T07:00:00+08:00",
"EpochDate":"1465254000",
"Sun_Rise":"2016-06-07T04:50:00+08:00",
"Sun_EpochRise":"1465246200",
"Sun_Set":"2016-06-07T18:57:00+08:00",
"Sun_EpochSet":"1465297020",
"PrecipitationProbability":"55"
},
{
"Date":"2016-06-08T07:00:00+08:00",
"EpochDate":"1465340400",
"Sun_Rise":"2016-06-08T04:50:00+08:00",
"Sun_EpochRise":"1465332600",
"Sun_Set":"2016-06-08T18:57:00+08:00",
"Sun_EpochSet":"1465383420",
"PrecipitationProbability":"55"
},
{
"Date":"2016-06-09T07:00:00+08:00",
"EpochDate":"1465426800",
"Sun_Rise":"2016-06-09T04:50:00+08:00",
"Sun_EpochRise":"1465419000",
"Sun_Set":"2016-06-09T18:58:00+08:00",
"Sun_EpochSet":"1465469880",
"PrecipitationProbability":"17"
},
{
"Date":"2016-06-10T07:00:00+08:00",
"EpochDate":"1465513200",
"Sun_Rise":"2016-06-10T04:50:00+08:00",
"Sun_EpochRise":"1465505400",
"Sun_Set":"2016-06-10T18:58:00+08:00",
"Sun_EpochSet":"1465556280",
"PrecipitationProbability":"55"
}
]
},
"today":{
"cityCode":"101020100",
"date":"2016-06-06",
"humidityMax":87,
"humidityMin":66,
"precipitationMax":0,
"precipitationMin":0,
"tempMax":26,
"tempMin":19,
"weatherEnd":"多雲",
"weatherStart":"多雲",
"windDirectionEnd":"東北風",
"windDirectionStart":"北風",
"windMax":1,
"windMin":0
},
"yestoday":{
"cityCode":"101020100",
"date":"2016-06-05",
"humidityMax":91,
"humidityMin":63,
"precipitationMax":0,
"precipitationMin":0,
"tempMax":24,
"tempMin":19,
"weatherEnd":"多雲",
"weatherStart":"多雲",
"windDirectionEnd":"東北風",
"windDirectionStart":"北風",
"windMax":1,
"windMin":0
}
}

是不是一下看了這數據就蒙了,我剛剛看的時候看的時候也是一樣,但是仔細看看對著其他天氣預報的app看下,就知道大概是什麼意思了,鞋面我簡單介紹下,json數據主要包括以下一個模塊,後面會分不同的模塊來解釋。

public class RawWeatherData {
    public ForecastInfo forecast;
    public RealTimeWeatherInfo realtime;
    public List alert;
    public AqiInfo aqi;
    public List index;
    public AccuCcInfo accu_cc;
    public AccuF5Info accu_f5;
    public TodayInfo today;
    public YestodayInfo yestoday;

}

public class AccuCcInfo {



    public String EpochTime;
    public String LocalObservationDateTime;
    public String Pressure; //氣壓
    public String RealFeelTemperature;//體感溫度
    public String RelativeHumidity;//相對濕度
    public String UVIndex;//紫外線強度
    public String Visibility;//可見度,單位KM
    public String WindDirectionDegrees;//風向程度,這個不確定
    public String WindSpeed;//風速

    public AccuCcInfo(String epochTime, String windSpeed, String windDirectionDegrees, String visibility, String UVIndex, String relativeHumidity, String realFeelTemperature, String pressure, String localObservationDateTime) {
        EpochTime = epochTime;
        WindSpeed = windSpeed;
        WindDirectionDegrees = windDirectionDegrees;
        Visibility = visibility;
        this.UVIndex = UVIndex;
        RelativeHumidity = relativeHumidity;
        RealFeelTemperature = realFeelTemperature;
        Pressure = pressure;
        LocalObservationDateTime = localObservationDateTime;
    }


}
public class AccuF5Info {


    public String EffectiveEpochDate;
    public String EffectiveDate;

    public AccuF5Info(String effectiveEpochDate, String effectiveDate, List dailyForecasts) {
        EffectiveEpochDate = effectiveEpochDate;
        EffectiveDate = effectiveDate;
        DailyForecasts = dailyForecasts;
    }

    public List DailyForecasts;

    public class DailyForecastsBean {
        public String Date;  //日期
        public String EpochDate;
        public String Sun_Rise;//日出時間
        public String Sun_EpochRise;
        public String Sun_Set;//日落時間
        public String Sun_EpochSet;
        public String PrecipitationProbability;//降雨概率

        public DailyForecastsBean(String date, String precipitationProbability, String sun_EpochSet, String sun_Set, String sun_Rise, String epochDate, String sun_EpochRise) {
            Date = date;
            PrecipitationProbability = precipitationProbability;
            Sun_EpochSet = sun_EpochSet;
            Sun_Set = sun_Set;
            Sun_Rise = sun_Rise;
            EpochDate = epochDate;
            Sun_EpochRise = sun_EpochRise;
        }
    }
}
public class AqiInfo {

    /**
     * city : 北京
     * city_id : 101010100
     * pub_time : 2016-05-09 08:00
     * aqi : 73
     * pm25 : 46
     * pm10 : 88
     * so2 : 7
     * no2 : 63
     * src : 中國環境監測總站
     * spot :
     */

    public String city;//城市名
    public String city_id;//城市id
    public String pub_time;//天氣預報推出的時間
    public String aqi;
    public String pm25;//pm25的值
    public String pm10;//pm10的值
    public String so2;
    public String no2;
    public String src;//信息來源
    public String spot;

    public AqiInfo(String city, String spot, String so2, String pm10, String pm25, String pub_time, String city_id, String aqi, String no2, String src) {
        this.city = city;
        this.spot = spot;
        this.so2 = so2;
        this.pm10 = pm10;
        this.pm25 = pm25;
        this.pub_time = pub_time;
        this.city_id = city_id;
        this.aqi = aqi;
        this.no2 = no2;
        this.src = src;
    }


}
public class ForecastInfo {

    public String city;//城市名
    public String city_en;//城市英文名
    public String cityid;//cityid
    public String date;
    public String date_y;//日期
    public String fchh;
    public String fl1;
    public String fl2;
    public String fl3;
    public String fl4;
    public String fl5;
    public String fl6;
    public String fx1;
    public String fx2;
    public String img1;//當天的天氣對應的圖片的名字
    public String img10;
    public String img11;
    public String img12;
    public String img2;//第二天天氣對應的圖片的名字
    public String img3;//第三天天氣對應的圖片的名字
    public String img4;//第四天天氣對應的圖片的名字
    public String img5;//第五天天氣對應的圖片的名字
    public String img6;
    public String img7;
    public String img8;
    public String img9;
    public String img_single;
    public String img_title1;
    public String img_title10;
    public String img_title11;
    public String img_title12;
    public String img_title2;
    public String img_title3;
    public String img_title4;
    public String img_title5;
    public String img_title6;
    public String img_title7;
    public String img_title8;
    public String img_title9;
    public String img_title_single;
    public String index;
    public String index48;
    public String index48_d;
    public String index48_uv;
    public String index_ag;
    public String index_cl;
    public String index_co;
    public String index_d;
    public String index_ls;
    public String index_tr;
    public String index_uv;
    public String index_xc;
    public String st1;
    public String st2;
    public String st3;
    public String st4;
    public String st5;
    public String st6;
    public String temp1;//當天的溫度范圍
    public String temp2;//第二天的溫度范圍
    public String temp3;//第三天的溫度范圍
    public String temp4;//第四天的溫度范圍
    public String temp5;//第五天的溫度范圍
    public String temp6;
    public String tempF1;
    public String tempF2;
    public String tempF3;
    public String tempF4;
    public String tempF5;
    public String tempF6;
    public String weather1;//當天的天氣描述如:多雲轉陣雨
    public String weather2;//第二天天氣描述
    public String weather3;//第三天天氣描述
    public String weather4;//第四天天氣描述
    public String weather5;//第五天天氣描述
    public String weather6;
    public String week;
    public String wind1;//當天風向
    public String wind2;//第二天風向
    public String wind3;//第三天風向
    public String wind4;//第四天風向
    public String wind5;//第四天風向
    public String wind6;

    public ForecastInfo(String city, String city_en, String cityid, String date, String date_y, String fchh, String fl1, String fl2, String fl3, String fl4, String fl5, String fl6, String fx2, String fx1, String img1, String img10, String img11, String img12, String img2, String img3, String img4, String img5, String img6, String img7, String img8, String img9, String img_single, String img_title1, String img_title10, String img_title11, String img_title12, String img_title2, String img_title3, String img_title4, String img_title5, String img_title6, String img_title7, String img_title8, String img_title9, String img_title_single, String index, String index48, String index48_d, String index48_uv, String index_ag, String index_cl, String index_co, String index_d, String index_ls, String index_tr, String index_uv, String index_xc, String st1, String st2, String st3, String st4, String st5, String st6, String temp1, String temp2, String temp3, String temp4, String temp5, String temp6, String tempF1, String tempF2, String tempF3, String tempF4, String tempF5, String tempF6, String weather1, String weather2, String weather3, String weather4, String weather5, String weather6, String week, String wind1, String wind2, String wind3, String wind4, String wind5, String wind6) {
        this.city = city;
        this.city_en = city_en;
        this.cityid = cityid;
        this.date = date;
        this.date_y = date_y;
        this.fchh = fchh;
        this.fl1 = fl1;
        this.fl2 = fl2;
        this.fl3 = fl3;
        this.fl4 = fl4;
        this.fl5 = fl5;
        this.fl6 = fl6;
        this.fx2 = fx2;
        this.fx1 = fx1;
        this.img1 = img1;
        this.img10 = img10;
        this.img11 = img11;
        this.img12 = img12;
        this.img2 = img2;
        this.img3 = img3;
        this.img4 = img4;
        this.img5 = img5;
        this.img6 = img6;
        this.img7 = img7;
        this.img8 = img8;
        this.img9 = img9;
        this.img_single = img_single;
        this.img_title1 = img_title1;
        this.img_title10 = img_title10;
        this.img_title11 = img_title11;
        this.img_title12 = img_title12;
        this.img_title2 = img_title2;
        this.img_title3 = img_title3;
        this.img_title4 = img_title4;
        this.img_title5 = img_title5;
        this.img_title6 = img_title6;
        this.img_title7 = img_title7;
        this.img_title8 = img_title8;
        this.img_title9 = img_title9;
        this.img_title_single = img_title_single;
        this.index = index;
        this.index48 = index48;
        this.index48_d = index48_d;
        this.index48_uv = index48_uv;
        this.index_ag = index_ag;
        this.index_cl = index_cl;
        this.index_co = index_co;
        this.index_d = index_d;
        this.index_ls = index_ls;
        this.index_tr = index_tr;
        this.index_uv = index_uv;
        this.index_xc = index_xc;
        this.st1 = st1;
        this.st2 = st2;
        this.st3 = st3;
        this.st4 = st4;
        this.st5 = st5;
        this.st6 = st6;
        this.temp1 = temp1;
        this.temp2 = temp2;
        this.temp3 = temp3;
        this.temp4 = temp4;
        this.temp5 = temp5;
        this.temp6 = temp6;
        this.tempF1 = tempF1;
        this.tempF2 = tempF2;
        this.tempF3 = tempF3;
        this.tempF4 = tempF4;
        this.tempF5 = tempF5;
        this.tempF6 = tempF6;
        this.weather1 = weather1;
        this.weather2 = weather2;
        this.weather3 = weather3;
        this.weather4 = weather4;
        this.weather5 = weather5;
        this.weather6 = weather6;
        this.week = week;
        this.wind1 = wind1;
        this.wind2 = wind2;
        this.wind3 = wind3;
        this.wind4 = wind4;
        this.wind5 = wind5;
        this.wind6 = wind6;
    }
}
public class IndexWeatherInfo {

    /**
     * code : fs
     * details : 屬弱紫外輻射天氣,長期在戶外,建議塗擦SPF在8-12之間的防曬護膚品。
     * index : 弱
     * name : 防曬指數、穿衣指數、運動指數等信息的介紹
     * otherName :
     */

    public String code;
    public String details;
    public String index;
    public String name;
    public String otherName;

    public IndexWeatherInfo(String code, String otherName, String name, String index, String details) {
        this.code = code;
        this.otherName = otherName;
        this.name = name;
        this.index = index;
        this.details = details;
    }
}
public class RealTimeWeatherInfo {
    public String SD;
    public String WD;
    public String WS;
    public String WSE;
    public String city;
    public String cityid;
    public String isRadar;
    public String radar;
    public String temp;//溫度
    public String time;//時間
    public String weather;

    public RealTimeWeatherInfo(String SD, String radar, String cityid, String city, String WSE, String WS, String WD, String isRadar, String temp, String time, String weather) {
        this.SD = SD;
        this.radar = radar;
        this.cityid = cityid;
        this.city = city;
        this.WSE = WSE;
        this.WS = WS;
        this.WD = WD;
        this.isRadar = isRadar;
        this.temp = temp;
        this.time = time;
        this.weather = weather;
    }


}
public class TodayInfo {



    public String cityCode; //城市id
    public String date;//日期
    public String humidityMax;//最大濕度
    public String humidityMin;//最小濕度
    public String precipitationMax;
    public String precipitationMin;
    public String tempMax;//最高溫度
    public String tempMin;//最低溫度
    public String weatherEnd;//結束的天氣
    public String weatherStart;//開始的天氣
    public String windDirectionEnd;//結束的風向
    public String windDirectionStart;//開始的風向
    public String windMax;//最大風速
    public String windMin;//最小風速

    public TodayInfo(String cityCode, String windMax, String windMin, String windDirectionStart, String weatherStart, String windDirectionEnd, String weatherEnd, String tempMin, String tempMax, String precipitationMin, String precipitationMax, String humidityMin, String humidityMax, String date) {
        this.cityCode = cityCode;
        this.windMax = windMax;
        this.windMin = windMin;
        this.windDirectionStart = windDirectionStart;
        this.weatherStart = weatherStart;
        this.windDirectionEnd = windDirectionEnd;
        this.weatherEnd = weatherEnd;
        this.tempMin = tempMin;
        this.tempMax = tempMax;
        this.precipitationMin = precipitationMin;
        this.precipitationMax = precipitationMax;
        this.humidityMin = humidityMin;
        this.humidityMax = humidityMax;
        this.date = date;
    }
}
public class YestodayInfo {


    public String cityCode;//城市id
    public String date;//日期
    public String humidityMax;//最大濕度
    public String humidityMin;//最小濕度
    public String precipitationMax;
    public String precipitationMin;
    public String tempMax;//最高溫度
    public String tempMin;//最低溫度
    public String weatherEnd;//結束的天氣
    public String weatherStart;//開始的天氣
    public String windDirectionEnd;//結束的風向
    public String windDirectionStart;//開始的風向
    public String windMax;//最大風速
    public String windMin;//最小風速

    public YestodayInfo(String cityCode, String windMin, String windMax, String windDirectionStart, String windDirectionEnd, String weatherStart, String weatherEnd, String tempMin, String tempMax, String precipitationMin, String precipitationMax, String humidityMin, String humidityMax, String date) {
        this.cityCode = cityCode;
        this.windMin = windMin;
        this.windMax = windMax;
        this.windDirectionStart = windDirectionStart;
        this.windDirectionEnd = windDirectionEnd;
        this.weatherStart = weatherStart;
        this.weatherEnd = weatherEnd;
        this.tempMin = tempMin;
        this.tempMax = tempMax;
        this.precipitationMin = precipitationMin;
        this.precipitationMax = precipitationMax;
        this.humidityMin = humidityMin;
        this.humidityMax = humidityMax;
        this.date = date;
    }
}

到這裡如果想實現上面兩張圖的效果,應該很容易了,代碼我就不全部貼出來了,有需要的可以去https://github.com/luyouxin/WeatherForecast上下載,有bug希望大家一起幫忙修改!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved