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