Android简单版天气预报源码,整体重构(第四步)

Android简单版天气预报源码,整体重构(第四步)

找到一个比较好用的天气数据API

http://wthrcdn.etouch.cn/weather_mini?city=深圳
http://wthrcdn.etouch.cn/weather_mini?citykey=101280601

  • 这两个都可以 我选择了第一个进行更新,不用之前的的心知天气API了

快春节了,更新一下,说是第四步,也不是哈哈哈哈

项目源码地址
https://github.com/LGH-cmd/weather-report

前端UI跟之前差不多

Android简单版天气预报源码,整体重构(第四步)
Android简单版天气预报源码,整体重构(第四步)

改动点1

改动了API,当然要重新写GSON

也就依葫芦画瓢,就不展示了
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;
    }

}

不同点,在预报中用的是ListView展示
也不是很难实现。与之前有相同之处,可参考前面,完整代码已经在GitHub上

改动点2

将可以选的城市扩充了

在strings.xml写入城市数组
Android简单版天气预报源码,整体重构(第四步)

<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>

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();
            }
        });

    }
}

更新了 比之前好多了 后面继续实现其他功能

Android简单版天气预报源码,整体重构(第四步)Android简单版天气预报源码,整体重构(第四步) wei_小灰灰 发布了17 篇原创文章 · 获赞 4 · 访问量 4149 私信 关注
上一篇:查找里程(MOOC面向对象程序设计--Java语言)


下一篇:R中数据框某一列加一个字