最近心情比较浮躁,项目初步已经完成一直没有心情来更新博客,基本功能已经实现了包括添加城市,删除城市,获取城市部分天气预报信息,已经详细的天气预报信息,还集成了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,List<CityInfo>list, 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<CityInfo> 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<CityInfo> 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<AlertInfo> alert;
public AqiInfo aqi;
public List<IndexWeatherInfo> 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<DailyForecastsBean> dailyForecasts) {
EffectiveEpochDate = effectiveEpochDate;
EffectiveDate = effectiveDate;
DailyForecasts = dailyForecasts;
}
public List<DailyForecastsBean> 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希望大家一起帮忙修改!