找到一个比较好用的天气数据API
这两个都可以 我选择了第一个进行更新,不用之前的的心知天气API了http://wthrcdn.etouch.cn/weather_mini?city=深圳@H_301_11@http://wthrcdn.etouch.cn/weather_mini?citykey=101280601
快春节了,更新一下,说是第四步,也不是哈哈哈哈
项目源码地址@H_301_11@https://github.com/LGH-cmd/weather-report
前端UI跟之前差不多
@H_301_11@改动点1改动了API,当然要重新写GSON
可以参考前面实现的@H_301_11@https://blog.csdn.net/weixin_44889138/article/details/102797849也就依葫芦画瓢,就不展示了@H_301_11@MainActivity 大部分实现也就是得到数据后展示到页面
整体代码
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private TextVIEw tvTitlename; // 城市名称 private TextVIEw tvNowDate; // 今天的日期 private TextVIEw tvNowTemp; // 今天的温度 private TextVIEw tvNowType; // 今天的天气类型 private TextVIEw tvNowWind; // 今天的风向 private TextVIEw tvNowGanmao; // 今天的提示 private button SwitchCity; // 切换其他城市 private ListVIEw lvForecast ; // 预报ListVIEw private List<ForecastData> dataList = new ArrayList<>(); // 预报列表 private ForecastAdapter adapter; // 预报适配器 @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); tvTitlename = findVIEwByID(R.ID.tv_Title_name); tvNowDate = findVIEwByID(R.ID.tv_Now_date); tvNowTemp = findVIEwByID(R.ID.tv_Now_temp); tvNowType = findVIEwByID(R.ID.tv_Now_type); tvNowWind = findVIEwByID(R.ID.tv_Now_wind); tvNowGanmao = findVIEwByID(R.ID.tv_Now_ganmao); SwitchCity = findVIEwByID(R.ID.btn_switch_city); lvForecast = findVIEwByID(R.ID.lv_forecast_content); GetDataAndShowData("深圳"); // 选择其他城市 SwitchCity.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { Intent intent = new Intent(MainActivity.this, SelectCityActivity.class); startActivityForResult(intent, 1001); } }); } @OverrIDe protected voID onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if(requestCode == 1001 && resultCode == 1002) { String cityname = data.getStringExtra("cityname"); GetDataAndShowData(cityname); } } private voID GetDataAndShowData(String cityname) { ProgressDialogUtil.showProgressDialog(MainActivity.this); // 加载框 String address = "http://wthrcdn.etouch.cn/weather_mini?city="+cityname; DataUtil.sendOkhttpRequest(address, new Callback() { @OverrIDe public voID onResponse(Call call, final Response response) throws IOException { runOnUiThread(new Runnable() { @OverrIDe public voID run() { try { WeatherResponse weatherData = DataUtil.getWeatherData(response.body().string()); String cityname = weatherData.getData().getCity(); // 城市名称 tvTitlename.setText(cityname); String wendu = weatherData.getData().getWendu(); // 今天的温度 tvNowTemp.setText(wendu+"℃"); String ganmao = weatherData.getData().getGanmao(); // 今天的提示 tvNowGanmao.setText(ganmao); List<Forecast> forecast = weatherData.getData().getForecast(); // 天气信息 ForecastData NowData = setNowWeatherData(forecast); // 今天的天气信息 // 设置其他日期的信息 // 昨天 Yesterday yesterday = weatherData.getData().getYesterday(); ForecastData yesterdayData = setYesterdayWeatherData(yesterday); // 未来几天 ForecastData futureOne = setotherDayWeatherData(forecast.get(1)); ForecastData futureTwo = setotherDayWeatherData(forecast.get(2)); ForecastData futureThree = setotherDayWeatherData(forecast.get(3)); ForecastData futureFour = setotherDayWeatherData(forecast.get(4)); if(dataList.size()!=0) { dataList.clear(); // 清除请一次的数据 } dataList.add(yesterdayData); dataList.add(NowData); dataList.add(futureOne);dataList.add(futureTwo);dataList.add(futureThree);dataList.add(futureFour); adapter = new ForecastAdapter(dataList); lvForecast.setAdapter(adapter); ProgressDialogUtil.dismiss(); // 消失加载框 } catch (IOException e) { e.printstacktrace(); } } }); } @OverrIDe public voID onFailure(Call call, IOException e) { Log.e(TAG, "获得天气出错了" + e); } }); } /** * 设置今天的天气信息,返回一个ForecastData,用于在预告中显示 * @param forecast * @return */ private ForecastData setNowWeatherData(List<Forecast> forecast) { Forecast forecastNow = forecast.get(0); // 今天的天气信息 String NowDate = forecastNow.getDate(); // 今天的日期 tvNowDate.setText("今天-"+NowDate); String NowTemp = forecastNow.getHigh(); // 今天的最高温度 String NowLow = forecastNow.getLow(); // 今天的最低温度 String NowType = forecastNow.getType(); // 今天的天气类型 tvNowType.setText(NowType); String NowWind = forecastNow.getFengxiang(); // 今天的风向 tvNowWind.setText(NowWind); ForecastData NowData = new ForecastData(NowDate, NowTemp.substring(2), NowLow.substring(2), NowType); // 今天的天气 return NowData; } /** * 设置昨天的信息 * @param yesterday * @return */ private ForecastData setYesterdayWeatherData(Yesterday yesterday) { String date = yesterday.getDate(); // 昨天的日期 String high = yesterday.getHigh(); // 昨天的最高温度 String low = yesterday.getLow(); // 昨天的最低温度 String type = yesterday.getType(); // 昨天的天气类型 ForecastData yesterdayData = new ForecastData(date, high.substring(2), low.substring(2), type); // 昨天的天气 return yesterdayData; } /** * 未来几天的天气 * @param forecast * @return */ private ForecastData setotherDayWeatherData(Forecast forecast) { String date = forecast.getDate(); // 未来几天的日期 String high = forecast.getHigh(); // 未来几天的最高温度 String low = forecast.getLow(); // 未来几天的最低温度 String type = forecast.getType(); // 未来几天的天气类型 ForecastData otherDayData = new ForecastData(date, high.substring(2), low.substring(2), type); // 未来几天的天气 return otherDayData; }}@H_404_50@
不同点,在预报中用的是ListVIEw展示@H_301_11@也不是很难实现。与之前有相同之处,可参考前面,完整代码已经在GitHub上
改动点2将可以选的城市扩充了
在strings.xml写入城市数组@H_301_11@
<string-array name="city_List"> <item>广州</item> <item>番禺</item> <item>从化</item> <item>增城</item> <item>花都</item> <item>韶关</item> <item>乳源</item> <item>始兴</item> <item>翁源</item> <item>乐昌</item> <item>仁化</item> <item>南雄</item> <item>新丰</item> <item>曲江</item> <item>浈江</item> <item>武江</item> <item>惠州</item> <item>博罗</item> <item>惠阳</item> <item>惠东</item> <item>龙门</item> <item>梅州</item> <item>兴宁</item> <item>蕉岭</item> <item>大埔</item> <item>丰顺</item> <item>平远</item> <item>五华</item> <item>梅县</item> <item>汕头</item> <item>潮阳</item> <item>澄海</item> <item>南澳</item> <item>深圳</item> <item>珠海</item> <item>斗门</item> <item>金湾</item> <item>佛山</item> <item>顺德</item> <item>三水</item> <item>南海</item> <item>高明</item> <item>肇庆</item> <item>广宁</item> <item>四会</item> <item>德庆</item> <item>怀集</item> <item>封开</item> <item>高要</item> <item>湛江</item> <item>吴川</item> <item>雷州</item> <item>徐闻</item> <item>廉江</item> <item>赤坎</item> <item>遂溪</item> <item>坡头</item> <item>霞山</item> <item>麻章</item> <item>江门</item> <item>开平</item> <item>新会</item> <item>恩平</item> <item>台山</item> <item>蓬江</item> <item>鹤山</item> <item>江海</item> <item>河源</item> <item>紫金</item> <item>连平</item> <item>和平</item> <item>龙川</item> <item>东源</item> <item>清远</item> <item>连南</item> <item>连州</item> <item>连山</item> <item>阳山</item> <item>佛冈</item> <item>英德</item> <item>清新</item> <item>云浮</item> <item>罗定</item> <item>新兴</item> <item>郁南</item> <item>云安</item> <item>潮州</item> <item>饶平</item> <item>潮安</item> <item>东莞</item> <item>中山</item> <item>阳江</item> <item>阳春</item> <item>阳东</item> <item>阳西</item> <item>揭阳</item> <item>揭西</item> <item>普宁</item> <item>惠来</item> <item>揭东</item> <item>茂名</item> <item>高州</item> <item>化州</item> <item>电白</item> <item>信宜</item> <item>茂港</item> <item>汕尾</item> <item>海丰</item> <item>陆丰</item> <item>陆河</item> </string-array>@H_404_50@
SelectCityActivity实现列表展示
public class SelectCityActivity extends AppCompatActivity { private ListVIEw lvCityList; private ArrayAdapter<String> adapter; private String[] cityList; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_select_city); lvCityList = findVIEwByID(R.ID.lv_city_List); cityList = getResources().getStringArray(R.array.city_List); adapter=new ArrayAdapter<>(SelectCityActivity.this,androID.R.layout.simple_List_item_1,cityList); lvCityList.setAdapter(adapter); lvCityList.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> adapterVIEw, VIEw vIEw, int i, long l) { String cityname = cityList[i]; Toast.makeText(SelectCityActivity.this,cityname,Toast.LENGTH_SHORT).show(); Intent intent = new Intent(); intent.putExtra("cityname",cityname); setResult(1002, intent); finish(); } }); }}@H_404_50@
更新了 比之前好多了 后面继续实现其他功能
点赞收藏分享文章举报wei_小灰灰发布了17 篇原创文章 · 获赞 4 · 访问量 4149私信 关注 总结以上是内存溢出为你收集整理的Android简单版天气预报源码,整体重构(第四步)全部内容,希望文章能够帮你解决Android简单版天气预报源码,整体重构(第四步)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)