Android实践之简易天气(一)
学习了好久的安卓,把各个组件基本都学完了,就想自己做个软件巩固一下学习的知识,顺便体验一下做项目的感觉,好了,废话不多说,下面开始进行软件的制作。
首先,要做天气软件,当然需要获取天气数据了,我用的是百度的API,这里顺便附上链接百度天气api,我下载的是SDK,这样就简化了许多。具体SDK的使用方法,下载的压缩包里面有,这里就不再赘述了,如果不会的话可以留言。
获取json数据
这里的获取的天气数据都是json形式,所以要进行json数据解析,你可以直接选择json解析,也可以使用gson和fastgson进行解析发,这里我使用的是直接json解析。
我们现在来看一下,从服务器返回的json数据。
{“HeWeather data service 3.0”:[{“aqi”:{“city”:{“aqi”:”82”,”pm10”:”83”,”pm25”:”60”}},”basic”:{“city”:”安阳”,”cnty”:”中国”,”id”:”CN101180201”,”lat”:”36.093000”,”lon”:”114.341000”,”update”:{“loc”:”2016-05-14 20:51”,”utc”:”2016-05-14 12:51”}},”daily_forecast”:[{“astro”:{“sr”:”05:17”,”ss”:”19:20”},”cond”:{“code_d”:”306”,”code_n”:”305”,”txt_d”:”中雨”,”txt_n”:”小雨”},”date”:”2016-05-14”,”hum”:”88”,”pcpn”:”18.0”,”pop”:”98”,”pres”:”1018”,”tmp”:{“max”:”16”,”min”:”11”},”vis”:”7”,”wind”:{“deg”:”15”,”dir”:”西北风”,”sc”:”微风”,”spd”:”2”}},{“astro”:{“sr”:”05:16”,”ss”:”19:21”},”cond”:{“code_d”:”100”,”code_n”:”100”,”txt_d”:”晴”,”txt_n”:”晴”},”date”:”2016-05-15”,”hum”:”13”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1018”,”tmp”:{“max”:”24”,”min”:”11”},”vis”:”10”,”wind”:{“deg”:”15”,”dir”:”西南风”,”sc”:”微风”,”spd”:”0”}},{“astro”:{“sr”:”05:15”,”ss”:”19:22”},”cond”:{“code_d”:”100”,”code_n”:”100”,”txt_d”:”晴”,”txt_n”:”晴”},”date”:”2016-05-16”,”hum”:”13”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1013”,”tmp”:{“max”:”29”,”min”:”13”},”vis”:”10”,”wind”:{“deg”:”194”,”dir”:”南风”,”sc”:”微风”,”spd”:”7”}},{“astro”:{“sr”:”05:14”,”ss”:”19:23”},”cond”:{“code_d”:”100”,”code_n”:”101”,”txt_d”:”晴”,”txt_n”:”多云”},”date”:”2016-05-17”,”hum”:”19”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1009”,”tmp”:{“max”:”29”,”min”:”16”},”vis”:”10”,”wind”:{“deg”:”195”,”dir”:”南风”,”sc”:”微风”,”spd”:”4”}},{“astro”:{“sr”:”05:14”,”ss”:”19:24”},”cond”:{“code_d”:”101”,”code_n”:”101”,”txt_d”:”多云”,”txt_n”:”多云”},”date”:”2016-05-18”,”hum”:”24”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1012”,”tmp”:{“max”:”27”,”min”:”15”},”vis”:”10”,”wind”:{“deg”:”165”,”dir”:”东南风”,”sc”:”微风”,”spd”:”6”}},{“astro”:{“sr”:”05:13”,”ss”:”19:24”},”cond”:{“code_d”:”101”,”code_n”:”305”,”txt_d”:”多云”,”txt_n”:”小雨”},”date”:”2016-05-19”,”hum”:”19”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1015”,”tmp”:{“max”:”27”,”min”:”16”},”vis”:”10”,”wind”:{“deg”:”126”,”dir”:”无持续风向”,”sc”:”微风”,”spd”:”5”}},{“astro”:{“sr”:”05:12”,”ss”:”19:25”},”cond”:{“code_d”:”305”,”code_n”:”101”,”txt_d”:”小雨”,”txt_n”:”多云”},”date”:”2016-05-20”,”hum”:”15”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1019”,”tmp”:{“max”:”23”,”min”:”16”},”vis”:”10”,”wind”:{“deg”:”99”,”dir”:”无持续风向”,”sc”:”微风”,”spd”:”5”}}],”hourly_forecast”:[{“date”:”2016-05-14 22:00”,”hum”:”91”,”pop”:”93”,”pres”:”1018”,”tmp”:”14”,”wind”:{“deg”:”264”,”dir”:”西风”,”sc”:”微风”,”spd”:”5”}}],”now”:{“cond”:{“code”:”300”,”txt”:”阵雨”},”fl”:”15”,”hum”:”88”,”pcpn”:”.2”,”pres”:”1018”,”tmp”:”13”,”vis”:”7”,”wind”:{“deg”:”343”,”dir”:”西南风”,”sc”:”4-5”,”spd”:”19”}},”status”:”ok”,”suggestion”:{“comf”:{“brf”:”舒适”,”txt”:”白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。”},”cw”:{“brf”:”不宜”,”txt”:”不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。”},”drsg”:{“brf”:”舒适”,”txt”:”建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。”},”flu”:{“brf”:”较易发”,”txt”:”昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。”},”sport”:{“brf”:”较适宜”,”txt”:”天气较好,但风力较大,推荐您进行室内运动,若在户外运动请注意防风。”},”trav”:{“brf”:”适宜”,”txt”:”天气较好,风稍大,但温度适宜,是个好天气哦。适宜旅游,您可以尽情地享受大自然的无限风光。”},”uv”:{“brf”:”中等”,”txt”:”属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。”}}},{“basic”:{“city”:”Anyang”,”cnty”:”Korea”,”id”:”KR1846898”,”lat”:”37.392502”,”lon”:”126.926941”,”update”:{“loc”:”2016-05-14 21:00”,”utc”:”2016-05-14 12:00”}},”daily_forecast”:[{“astro”:{“sr”:”05:24”,”ss”:”19:34”},”cond”:{“code_d”:”100”,”code_n”:”103”,”txt_d”:”Sunny/Clear”,”txt_n”:”Partly Cloudy”},”date”:”2016-05-14”,”hum”:”54”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1025”,”tmp”:{“max”:”23”,”min”:”14”},”vis”:”10”,”wind”:{“deg”:”127”,”dir”:”SE”,”sc”:”1-2”,”spd”:”13”}},{“astro”:{“sr”:”05:24”,”ss”:”19:35”},”cond”:{“code_d”:”100”,”code_n”:”306”,”txt_d”:”Sunny/Clear”,”txt_n”:”Moderate Rain”},”date”:”2016-05-15”,”hum”:”68”,”pcpn”:”23.5”,”pop”:”88”,”pres”:”1016”,”tmp”:{“max”:”22”,”min”:”11”},”vis”:”10”,”wind”:{“deg”:”134”,”dir”:”SE”,”sc”:”1-2”,”spd”:”13”}},{“astro”:{“sr”:”05:23”,”ss”:”19:36”},”cond”:{“code_d”:”100”,”code_n”:”100”,”txt_d”:”Sunny/Clear”,”txt_n”:”Sunny/Clear”},”date”:”2016-05-16”,”hum”:”69”,”pcpn”:”8.9”,”pop”:”84”,”pres”:”1008”,”tmp”:{“max”:”20”,”min”:”16”},”vis”:”10”,”wind”:{“deg”:”325”,”dir”:”NW”,”sc”:”1-2”,”spd”:”13”}},{“astro”:{“sr”:”05:22”,”ss”:”19:36”},”cond”:{“code_d”:”100”,”code_n”:”100”,”txt_d”:”Sunny/Clear”,”txt_n”:”Sunny/Clear”},”date”:”2016-05-17”,”hum”:”57”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1016”,”tmp”:{“max”:”24”,”min”:”13”},”vis”:”10”,”wind”:{“deg”:”190”,”dir”:”S”,”sc”:”1-2”,”spd”:”13”}},{“astro”:{“sr”:”05:21”,”ss”:”19:37”},”cond”:{“code_d”:”100”,”code_n”:”103”,”txt_d”:”Sunny/Clear”,”txt_n”:”Partly Cloudy”},”date”:”2016-05-18”,”hum”:”50”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1019”,”tmp”:{“max”:”26”,”min”:”16”},”vis”:”10”,”wind”:{“deg”:”1”,”dir”:”N”,”sc”:”1-2”,”spd”:”13”}},{“astro”:{“sr”:”05:20”,”ss”:”19:38”},”cond”:{“code_d”:”103”,”code_n”:”100”,”txt_d”:”Partly Cloudy”,”txt_n”:”Sunny/Clear”},”date”:”2016-05-19”,”hum”:”57”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1021”,”tmp”:{“max”:”30”,”min”:”21”},”vis”:”10”,”wind”:{“deg”:”55”,”dir”:”NE”,”sc”:”1-2”,”spd”:”13”}},{“astro”:{“sr”:”05:20”,”ss”:”19:39”},”cond”:{“code_d”:”100”,”code_n”:”100”,”txt_d”:”Sunny/Clear”,”txt_n”:”Sunny/Clear”},”date”:”2016-05-20”,”hum”:”52”,”pcpn”:”0.0”,”pop”:”0”,”pres”:”1022”,”tmp”:{“max”:”31”,”min”:”21”},”vis”:”10”,”wind”:{“deg”:”34”,”dir”:”NE”,”sc”:”1-2”,”spd”:”13”}}],”hourly_forecast”:[{“date”:”2016-05-14 22:00”,”hum”:”76”,”pop”:”0”,”pres”:”1021”,”tmp”:”15”,”wind”:{“deg”:”264”,”dir”:”W”,”sc”:”1-2”,”spd”:”4”}}],”now”:{“cond”:{“code”:”103”,”txt”:”Partly Cloudy”},”fl”:”24”,”hum”:”46”,”pcpn”:”0.0”,”pres”:”1018”,”tmp”:”22”,”vis”:”10”,”wind”:{“deg”:”300”,”dir”:”WNW”,”sc”:”1-2”,”spd”:”13”}},”status”:”ok”}]}
是不是感觉特别复杂,所以呢你要是进行全部解析,还是推荐你用gson或fastgson的方式进行解析。Android studio里面有gson插件,使用起来也比较方便,不过要注意像这种“HeWeather data service 3.0” 一定要没有空格才能利用Android studio中的gson插件生成实体类,不然就会提示解析错误。
定义实体类
这里我只解析我要用到的数据,既然要解析,肯定需要一个实体类了,我定义的实体类内容如下:
package com.baidu.apistore.sdk.bena;
import java.util.List;
/**
* Created by xiaohui on 2016/5/15.
*/
public class WeatherBean {
private List<OneDayWeatherInfs> mOneDayWeatherInfs;
private int now;
private int aqi;
private int pm10;
private int pm25;
private String city;
private String cond_txt;
private String cnty;
private String comfBrf;
private String comfTxt;
private String cwBrf;
private String cwTxt;
private String drsgBrf;
private String drsgTxt;
private String fluBrf;
private String fluTxt;
private String sportBrf;
private String sportTxt;
private String travBrf;
private String travTxt;
private String uvBrf;
private String uvTxt;
public WeatherBean() {
}
public WeatherBean(int aqi, String city, String cnty, String comfBrf, String comfTxt, String cond_txt, String cwBrf, String cwTxt, String drsgBrf, String drsgTxt, String fluBrf, String fluTxt, List<OneDayWeatherInfs> oneDayWeatherInfs, int now, int pm10, int pm25, String sportBrf, String sportTxt, String travBrf, String travTxt, String uvBrf, String uvTxt) {
this.aqi = aqi;
this.city = city;
this.cnty = cnty;
this.comfBrf = comfBrf;
this.comfTxt = comfTxt;
this.cond_txt = cond_txt;
this.cwBrf = cwBrf;
this.cwTxt = cwTxt;
this.drsgBrf = drsgBrf;
this.drsgTxt = drsgTxt;
this.fluBrf = fluBrf;
this.fluTxt = fluTxt;
mOneDayWeatherInfs = oneDayWeatherInfs;
this.now = now;
this.pm10 = pm10;
this.pm25 = pm25;
this.sportBrf = sportBrf;
this.sportTxt = sportTxt;
this.travBrf = travBrf;
this.travTxt = travTxt;
this.uvBrf = uvBrf;
this.uvTxt = uvTxt;
}
public int getAqi() {
return aqi;
}
public void setAqi(int aqi) {
this.aqi = aqi;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCnty() {
return cnty;
}
public void setCnty(String cnty) {
this.cnty = cnty;
}
public String getComfBrf() {
return comfBrf;
}
public void setComfBrf(String comfBrf) {
this.comfBrf = comfBrf;
}
public String getComfTxt() {
return comfTxt;
}
public void setComfTxt(String comfTxt) {
this.comfTxt = comfTxt;
}
public String getCond_txt() {
return cond_txt;
}
public void setCond_txt(String cond_txt) {
this.cond_txt = cond_txt;
}
public String getCwBrf() {
return cwBrf;
}
public void setCwBrf(String cwBrf) {
this.cwBrf = cwBrf;
}
public String getCwTxt() {
return cwTxt;
}
public void setCwTxt(String cwTxt) {
this.cwTxt = cwTxt;
}
public String getDrsgBrf() {
return drsgBrf;
}
public void setDrsgBrf(String drsgBrf) {
this.drsgBrf = drsgBrf;
}
public String getDrsgTxt() {
return drsgTxt;
}
public void setDrsgTxt(String drsgTxt) {
this.drsgTxt = drsgTxt;
}
public String getFluBrf() {
return fluBrf;
}
public void setFluBrf(String fluBrf) {
this.fluBrf = fluBrf;
}
public String getFluTxt() {
return fluTxt;
}
public void setFluTxt(String fluTxt) {
this.fluTxt = fluTxt;
}
public List<OneDayWeatherInfs> getOneDayWeatherInfs() {
return mOneDayWeatherInfs;
}
public void setOneDayWeatherInfs(List<OneDayWeatherInfs> oneDayWeatherInfs) {
mOneDayWeatherInfs = oneDayWeatherInfs;
}
public int getNow() {
return now;
}
public void setNow(int now) {
this.now = now;
}
public int getPm10() {
return pm10;
}
public void setPm10(int pm10) {
this.pm10 = pm10;
}
public int getPm25() {
return pm25;
}
public void setPm25(int pm25) {
this.pm25 = pm25;
}
public String getSportBrf() {
return sportBrf;
}
public void setSportBrf(String sportBrf) {
this.sportBrf = sportBrf;
}
public String getSportTxt() {
return sportTxt;
}
public void setSportTxt(String sportTxt) {
this.sportTxt = sportTxt;
}
public String getTravBrf() {
return travBrf;
}
public void setTravBrf(String travBrf) {
this.travBrf = travBrf;
}
public String getTravTxt() {
return travTxt;
}
public void setTravTxt(String travTxt) {
this.travTxt = travTxt;
}
public String getUvBrf() {
return uvBrf;
}
public void setUvBrf(String uvBrf) {
this.uvBrf = uvBrf;
}
public String getUvTxt() {
return uvTxt;
}
public void setUvTxt(String uvTxt) {
this.uvTxt = uvTxt;
}
@Override
public String toString() {
return "WeatherBean{" +
"aqi=" + aqi +
", mOneDayWeatherInfs=" + mOneDayWeatherInfs +
", now=" + now +
", pm10=" + pm10 +
", pm25=" + pm25 +
", city=‘" + city + ‘\‘‘ +
", cond_txt=‘" + cond_txt + ‘\‘‘ +
", cnty=‘" + cnty + ‘\‘‘ +
", comfBrf=‘" + comfBrf + ‘\‘‘ +
", comfTxt=‘" + comfTxt + ‘\‘‘ +
", cwBrf=‘" + cwBrf + ‘\‘‘ +
", cwTxt=‘" + cwTxt + ‘\‘‘ +
", drsgBrf=‘" + drsgBrf + ‘\‘‘ +
", drsgTxt=‘" + drsgTxt + ‘\‘‘ +
", fluBrf=‘" + fluBrf + ‘\‘‘ +
", fluTxt=‘" + fluTxt + ‘\‘‘ +
", sportBrf=‘" + sportBrf + ‘\‘‘ +
", sportTxt=‘" + sportTxt + ‘\‘‘ +
", travBrf=‘" + travBrf + ‘\‘‘ +
", travTxt=‘" + travTxt + ‘\‘‘ +
", uvBrf=‘" + uvBrf + ‘\‘‘ +
", uvTxt=‘" + uvTxt + ‘\‘‘ +
‘}‘;
}
}
我们会发现里面List 的泛型是OneDayWeatherInfs,所以还需要一个OneDayWeatherInfs 类,其内容如下:
package com.baidu.apistore.sdk.bena;
/**
* Created by xiaohui on 2016/5/15.
*/
public class OneDayWeatherInfs {
private String condD;
private String condN;
private String date;
private String windDir;
private String windSc;
private String windSpd;
private int tmpMax;
private int tmpMin;
public OneDayWeatherInfs() {
}
public OneDayWeatherInfs(String condD, String condN, String date, int tmpMax, int tmpMin, String windDir, String windSc, String windSpd) {
this.condD = condD;
this.condN = condN;
this.date = date;
this.tmpMax = tmpMax;
this.tmpMin = tmpMin;
this.windDir = windDir;
this.windSc = windSc;
this.windSpd = windSpd;
}
public String getCondD() {
return condD;
}
public void setCondD(String condD) {
this.condD = condD;
}
public String getCondN() {
return condN;
}
public void setCondN(String condN) {
this.condN = condN;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getTmpMax() {
return tmpMax;
}
public void setTmpMax(int tmpMax) {
this.tmpMax = tmpMax;
}
public int getTmpMin() {
return tmpMin;
}
public void setTmpMin(int tmpMin) {
this.tmpMin = tmpMin;
}
public String getWindDir() {
return windDir;
}
public void setWindDir(String windDir) {
this.windDir = windDir;
}
public String getWindSc() {
return windSc;
}
public void setWindSc(String windSc) {
this.windSc = windSc;
}
public String getWindSpd() {
return windSpd;
}
public void setWindSpd(String windSpd) {
this.windSpd = windSpd;
}
@Override
public String toString() {
return "OneDayWeatherInfs{" +
"condD=‘" + condD + ‘\‘‘ +
", condN=‘" + condN + ‘\‘‘ +
", date=‘" + date + ‘\‘‘ +
", windDir=‘" + windDir + ‘\‘‘ +
", windSc=‘" + windSc + ‘\‘‘ +
", windSpd=‘" + windSpd + ‘\‘‘ +
", tmpMax=" + tmpMax +
", tmpMin=" + tmpMin +
‘}‘;
}
}
解析json数据
好了实体类已经写好,下面就要进行json解析了,解析json的类如下:
package com.baidu.apistore.sdk.prase;
import com.baidu.apistore.sdk.bena.OneDayWeatherInfs;
import com.baidu.apistore.sdk.bena.WeatherBean;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* Created by xiaohui on 2016/5/15.
*/
public class Parse {
public Parse() {
}
public static WeatherBean resolveWeatherInf(String strPar) {
WeatherBean weatherBean = new WeatherBean();
try {
JSONObject dataOfJson = new JSONObject(strPar);
JSONArray jsonArray = dataOfJson.getJSONArray("HeWeather data service 3.0");
//空气质量解析
JSONObject jsonArray0 = jsonArray.getJSONObject(0);
JSONObject aqi = jsonArray0.getJSONObject("aqi");
JSONObject city = aqi.getJSONObject("city");
weatherBean.setAqi(city.getInt("aqi"));
weatherBean.setPm10(city.getInt("pm10"));
weatherBean.setPm25(city.getInt("pm25"));
JSONObject basic = jsonArray0.getJSONObject("basic");
weatherBean.setCity(basic.getString("city"));
weatherBean.setCnty(basic.getString("cnty"));
//当前温度
JSONObject now = jsonArray0.getJSONObject("now");
JSONObject nowCond = now.getJSONObject("cond");
weatherBean.setNow(now.getInt("tmp"));
weatherBean.setCond_txt(nowCond.getString("txt"));
//连续七天的天气数据解析
List<OneDayWeatherInfs> list = new ArrayList<>();
JSONArray daily_forecast = jsonArray0.getJSONArray("daily_forecast");
for (int i = 0; i < 7; i++) {
OneDayWeatherInfs oneDayWeatherInfs = new OneDayWeatherInfs();
JSONObject oneDayWeather = daily_forecast.getJSONObject(i);
JSONObject cond = oneDayWeather.getJSONObject("cond");
JSONObject temp = oneDayWeather.getJSONObject("tmp");
JSONObject wind = oneDayWeather.getJSONObject("wind");
oneDayWeatherInfs.setWindDir(wind.getString("dir"));
oneDayWeatherInfs.setWindSc(wind.getString("sc"));
oneDayWeatherInfs.setWindSpd(wind.getString("spd"));
oneDayWeatherInfs.setTmpMax(temp.getInt("max"));
oneDayWeatherInfs.setTmpMin(temp.getInt("min"));
oneDayWeatherInfs.setCondD(cond.getString("txt_d"));
oneDayWeatherInfs.setCondN(cond.getString("txt_n"));
oneDayWeatherInfs.setDate(oneDayWeather.getString("date"));
list.add(oneDayWeatherInfs);
}
weatherBean.setOneDayWeatherInfs(list);
//舒适度指数解析
JSONObject suggesttion = jsonArray0.getJSONObject("suggestion");
JSONObject comf = suggesttion.getJSONObject("comf");
weatherBean.setComfBrf(comf.getString("brf"));
weatherBean.setComfTxt(comf.getString("txt"));
JSONObject cw = suggesttion.getJSONObject("cw");
weatherBean.setCwBrf(cw.getString("brf"));
weatherBean.setCwTxt(cw.getString("txt"));
JSONObject drsg = suggesttion.getJSONObject("drsg");
weatherBean.setDrsgBrf(drsg.getString("brf"));
weatherBean.setDrsgTxt(drsg.getString("txt"));
JSONObject flu = suggesttion.getJSONObject("flu");
weatherBean.setFluBrf(flu.getString("brf"));
weatherBean.setFluTxt(flu.getString("txt"));
JSONObject sport = suggesttion.getJSONObject("sport");
weatherBean.setSportBrf(sport.getString("brf"));
weatherBean.setSportTxt(sport.getString("txt"));
JSONObject trav = suggesttion.getJSONObject("trav");
weatherBean.setTravBrf(trav.getString("brf"));
weatherBean.setTravTxt(trav.getString("txt"));
JSONObject uv = suggesttion.getJSONObject("uv");
weatherBean.setUvBrf(uv.getString("brf"));
weatherBean.setUvTxt(uv.getString("txt"));
} catch (JSONException e) {
e.printStackTrace();
}
return weatherBean;
}
}
上面就是进行json数据解析的类,然后就是解析服务器返回来的数据了,具体内容如下:
/**
* 这里的apiTest()方法是百度SDK自带的,返回的是json数据,
* 我们只要在onSuccess方法里面添加解析json数据的方法就好了。
*/
private void apiTest() {
final Parameters para = new Parameters();
para.put("city", "安阳");
ApiStoreSDK.execute("http://apis.baidu.com/heweather/weather/free",
ApiStoreSDK.GET,
para,
new ApiCallBack() {
@Override
public void onSuccess(int status, String responseString) {
Log.i("sdkdemo", "onSuccess");
Log.i("sdkdemo", "responseString" + responseString);
WeatherBean weatherBean = null;
Parse parse = new Parse();
weatherBean = parse.resolveWeatherInf(responseString);
}
@Override
public void onComplete() {
Log.i("sdkdemo", "onComplete");
}
@Override
public void onError(int status, String responseString, Exception e) {
Log.i("sdkdemo", "onError, status: " + status);
Log.i("sdkdemo", "errMsg: " + (e == null ? "" : e.getMessage()));
Toast.makeText(MainActivity.this, "" + getStackTrace(e), Toast.LENGTH_SHORT).show();
}
});
}
解析出来的数据如下:
05-25 20:10:02.121 5162-5162/com.baidu.apistore.sdk I/fnhFire: weatherBean—>WeatherBean{aqi=51, mOneDayWeatherInfs=[OneDayWeatherInfs{condD=’晴’, condN=’阴’, date=’2016-05-25’, windDir=’西北风’, windSc=’3-4’, windSpd=’16’, tmpMax=33, tmpMin=18}, OneDayWeatherInfs{condD=’多云’, condN=’多云’, date=’2016-05-26’, windDir=’南风’, windSc=’微风’, windSpd=’2’, tmpMax=29, tmpMin=18}, OneDayWeatherInfs{condD=’小雨’, condN=’小雨’, date=’2016-05-27’, windDir=’南风’, windSc=’微风’, windSpd=’10’, tmpMax=23, tmpMin=15}, OneDayWeatherInfs{condD=’多云’, condN=’多云’, date=’2016-05-28’, windDir=’南风’, windSc=’微风’, windSpd=’8’, tmpMax=29, tmpMin=17}, OneDayWeatherInfs{condD=’多云’, condN=’多云’, date=’2016-05-29’, windDir=’东北风’, windSc=’微风’, windSpd=’7’, tmpMax=32, tmpMin=19}, OneDayWeatherInfs{condD=’多云’, condN=’阴’, date=’2016-05-30’, windDir=’无持续风向’, windSc=’微风’, windSpd=’7’, tmpMax=33, tmpMin=20}, OneDayWeatherInfs{condD=’小雨’, condN=’小雨’, date=’2016-05-31’, windDir=’无持续风向’, windSc=’微风’, windSpd=’5’, tmpMax=29, tmpMin=20}], now=23, pm10=50, pm25=11, city=’安阳’, cond_txt=’多云’, cnty=’中国’, comfBrf=’暂缺’, comfTxt=’暂缺’, cwBrf=’较适宜’, cwTxt=’较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。’, drsgBrf=’暂缺’, drsgTxt=’暂缺’, fluBrf=’暂缺’, fluTxt=’暂缺’, sportBrf=’暂缺’, sportTxt=’暂缺’, travBrf=’暂缺’, travTxt=’暂缺’, uvBrf=’暂缺’, uvTxt=’暂缺’}
额(⊙o⊙)…,也不知道怎么回事,在写这篇博客的时候,上面的好多数据都出现了暂缺,我能保证在这以前肯定是可以的。就先这样吧,应该是百度API发神经了,好了,到这里我们的json数据解析方面已经搞定了,下篇就是界面设计和将解析的数据放在界面上了。