目录
一、效果展示
二、代码实现
1、网络权限配置
<uses-permission android:name="android.permission.INTERNET"/>
2、工具类HtmlService
public class HtmlService {
public static String getHtml(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();//通过输入流获取html数据
byte[] data = readInputStream(inStream);//得到html的二进制数据
String html = new String(data, "UTF-8");
return html;
}
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
3、请求数据
@Override
public void onClick(View view) {
if (view == null) {
return;
}
if (R.id.tvGetWeather == view.getId()) {
new Thread(runnable).start();
}
}
Runnable runnable = this::getWeather;
private void getWeather() {
String path = "http://t.weather.sojson.com/api/weather/city/101030100";
try {
String html = HtmlService.getHtml(path);
Log.e("kk", "html : " + html);
WeatherBean weatherBean = JsonUtil.fromJsonToObject(html, WeatherBean.class);
if (weatherBean == null) {
Log.e("kk", "weatherBean is null");
return;
}
mWeatherInfo = weatherBean.toString();
Log.v("kk", mWeatherInfo);
mHandler.removeMessages(MSG_UPDATE_WEATHER_INFO);
mHandler.sendEmptyMessageAtTime(MSG_UPDATE_WEATHER_INFO, MSG_UPDATE_WEATHER_INFO_DELAY);
} catch (Exception e) {
e.printStackTrace();
}
}
此时运行程序,查看log:
4、新建一个数据类命名为WeatherBean
将日志中的json数据通过GsonFormat进行格式化处理,快速生成数据类
package com.tzbc.test.bean;
import java.io.Serializable;
import java.util.List;
/**
* Created by tzbc on 2022/8/9.
*
* @author tzbc
*/
public class WeatherBean implements Serializable {
/**
* message : success感谢又拍云(upyun.com)提供CDN赞助
* status : 200
* date : 20220809
* time : 2022-08-09 15:00:56
* cityInfo : {"city":"天津市","citykey":"101030100","parent":"天津","updateTime":"14:31"}
* data : {"shidu":"85%","pm25":10,"pm10":21,"quality":"优","wendu":"24","ganmao":"各类人群可自由活动","forecast":[{"date":"09","high":"高温 26℃","low":"低温 20℃","ymd":"2022-08-09","week":"星期二","sunrise":"05:19","sunset":"19:14","aqi":22,"fx":"北风","fl":"2级","type":"中雨","notice":"记得随身携带雨伞哦"},{"date":"10","high":"高温 30℃","low":"低温 24℃","ymd":"2022-08-10","week":"星期三","sunrise":"05:20","sunset":"19:13","aqi":49,"fx":"西南风","fl":"2级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"11","high":"高温 31℃","low":"低温 24℃","ymd":"2022-08-11","week":"星期四","sunrise":"05:21","sunset":"19:12","aqi":68,"fx":"西南风","fl":"2级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"12","high":"高温 30℃","low":"低温 23℃","ymd":"2022-08-12","week":"星期五","sunrise":"05:22","sunset":"19:10","aqi":57,"fx":"东风","fl":"2级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"13","high":"高温 31℃","low":"低温 23℃","ymd":"2022-08-13","week":"星期六","sunrise":"05:23","sunset":"19:09","aqi":52,"fx":"东风","fl":"2级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":"14","high":"高温 30℃","low":"低温 22℃","ymd":"2022-08-14","week":"星期日","sunrise":"05:24","sunset":"19:08","aqi":42,"fx":"南风","fl":"2级","type":"中雨","notice":"记得随身携带雨伞哦"},{"date":"15","high":"高温 30℃","low":"低温 24℃","ymd":"2022-08-15","week":"星期一","sunrise":"05:25","sunset":"19:06","aqi":28,"fx":"北风","fl":"3级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"16","high":"高温 33℃","low":"低温 24℃","ymd":"2022-08-16","week":"星期二","sunrise":"05:25","sunset":"19:05","aqi":39,"fx":"北风","fl":"2级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"17","high":"高温 34℃","low":"低温 21℃","ymd":"2022-08-17","week":"星期三","sunrise":"05:26","sunset":"19:04","aqi":64,"fx":"南风","fl":"2级","type":"阴","notice":"不要被阴云遮挡住好心情"},{"date":"18","high":"高温 30℃","low":"低温 17℃","ymd":"2022-08-18","week":"星期四","sunrise":"05:27","sunset":"19:02","aqi":72,"fx":"南风","fl":"2级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":"19","high":"高温 29℃","low":"低温 19℃","ymd":"2022-08-19","week":"星期五","sunrise":"05:28","sunset":"19:01","aqi":69,"fx":"东北风","fl":"2级","type":"阴","notice":"不要被阴云遮挡住好心情"},{"date":"20","high":"高温 27℃","low":"低温 21℃","ymd":"2022-08-20","week":"星期六","sunrise":"05:29","sunset":"19:00","aqi":42,"fx":"东南风","fl":"2级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":"21","high