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
或:https://gitee.com/lin_g_g_hui/weather-report

前端UI跟之前差不多

在这里插入图片描述
在这里插入图片描述

改动点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
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值