目录
一、效果展示
二、代码实现
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":"高温 29℃","low":"低温 22℃","ymd":"2022-08-21","week":"星期日","sunrise":"05:30","sunset":"18:58","aqi":29,"fx":"北风","fl":"3级","type":"阴","notice":"不要被阴云遮挡住好心情"},{"date":"22","high":"高温 27℃","low":"低温 23℃","ymd":"2022-08-22","week":"星期一","sunrise":"05:31","sunset":"18:57","aqi":34,"fx":"东北风","fl":"2级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"23","high":"高温 33℃","low":"低温 25℃","ymd":"2022-08-23","week":"星期二","sunrise":"05:32","sunset":"18:55","aqi":29,"fx":"东北风","fl":"1级","type":"阴","notice":"不要被阴云遮挡住好心情"}],"yesterday":{"date":"08","high":"高温 30℃","low":"低温 22℃","ymd":"2022-08-08","week":"星期一","sunrise":"05:18","sunset":"19:15","aqi":15,"fx":"东北风","fl":"3级","type":"阴","notice":"不要被阴云遮挡住好心情"}}
*/
private String message;
private int status;
private String date;
private String time;
private CityInfoBean cityInfo;
private DataBean data;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public CityInfoBean getCityInfo() {
return cityInfo;
}
public void setCityInfo(CityInfoBean cityInfo) {
this.cityInfo = cityInfo;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public static class CityInfoBean implements Serializable {
/**
* city : 天津市
* citykey : 101030100
* parent : 天津
* updateTime : 14:31
*/
private String city;
private String citykey;
private String parent;
private String updateTime;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCitykey() {
return citykey;
}
public void setCitykey(String citykey) {
this.citykey = citykey;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "CityInfoBean{" +
"city='" + city + '\n' +
", citykey='" + citykey + '\n' +
", parent='" + parent + '\n' +
", updateTime='" + updateTime + '\n' +
'}';
}
}
public static class DataBean implements Serializable {
/**
* 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":"高温 29℃","low":"低温 22℃","ymd":"2022-08-21","week":"星期日","sunrise":"05:30","sunset":"18:58","aqi":29,"fx":"北风","fl":"3级","type":"阴","notice":"不要被阴云遮挡住好心情"},{"date":"22","high":"高温 27℃","low":"低温 23℃","ymd":"2022-08-22","week":"星期一","sunrise":"05:31","sunset":"18:57","aqi":34,"fx":"东北风","fl":"2级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},{"date":"23","high":"高温 33℃","low":"低温 25℃","ymd":"2022-08-23","week":"星期二","sunrise":"05:32","sunset":"18:55","aqi":29,"fx":"东北风","fl":"1级","type":"阴","notice":"不要被阴云遮挡住好心情"}]
* yesterday : {"date":"08","high":"高温 30℃","low":"低温 22℃","ymd":"2022-08-08","week":"星期一","sunrise":"05:18","sunset":"19:15","aqi":15,"fx":"东北风","fl":"3级","type":"阴","notice":"不要被阴云遮挡住好心情"}
*/
private String shidu;
private int pm25;
private int pm10;
private String quality;
private String wendu;
private String ganmao;
private YesterdayBean yesterday;
private List<ForecastBean> forecast;
public String getShidu() {
return shidu;
}
public void setShidu(String shidu) {
this.shidu = shidu;
}
public int getPm25() {
return pm25;
}
public void setPm25(int pm25) {
this.pm25 = pm25;
}
public int getPm10() {
return pm10;
}
public void setPm10(int pm10) {
this.pm10 = pm10;
}
public String getQuality() {
return quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
public String getWendu() {
return wendu;
}
public void setWendu(String wendu) {
this.wendu = wendu;
}
public String getGanmao() {
return ganmao;
}
public void setGanmao(String ganmao) {
this.ganmao = ganmao;
}
public YesterdayBean getYesterday() {
return yesterday;
}
public void setYesterday(YesterdayBean yesterday) {
this.yesterday = yesterday;
}
public List<ForecastBean> getForecast() {
return forecast;
}
public void setForecast(List<ForecastBean> forecast) {
this.forecast = forecast;
}
public static class YesterdayBean implements Serializable {
/**
* date : 08
* high : 高温 30℃
* low : 低温 22℃
* ymd : 2022-08-08
* week : 星期一
* sunrise : 05:18
* sunset : 19:15
* aqi : 15
* fx : 东北风
* fl : 3级
* type : 阴
* notice : 不要被阴云遮挡住好心情
*/
private String date;
private String high;
private String low;
private String ymd;
private String week;
private String sunrise;
private String sunset;
private int aqi;
private String fx;
private String fl;
private String type;
private String notice;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public String getYmd() {
return ymd;
}
public void setYmd(String ymd) {
this.ymd = ymd;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public String getSunrise() {
return sunrise;
}
public void setSunrise(String sunrise) {
this.sunrise = sunrise;
}
public String getSunset() {
return sunset;
}
public void setSunset(String sunset) {
this.sunset = sunset;
}
public int getAqi() {
return aqi;
}
public void setAqi(int aqi) {
this.aqi = aqi;
}
public String getFx() {
return fx;
}
public void setFx(String fx) {
this.fx = fx;
}
public String getFl() {
return fl;
}
public void setFl(String fl) {
this.fl = fl;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNotice() {
return notice;
}
public void setNotice(String notice) {
this.notice = notice;
}
@Override
public String toString() {
return "YesterdayBean{" +
"date='" + date + '\n' +
", high='" + high + '\n' +
", low='" + low + '\n' +
", ymd='" + ymd + '\n' +
", week='" + week + '\n' +
", sunrise='" + sunrise + '\n' +
", sunset='" + sunset + '\n' +
", aqi=" + aqi +
", fx='" + fx + '\n' +
", fl='" + fl + '\n' +
", type='" + type + '\n' +
", notice='" + notice + '\n' +
'}';
}
}
public static class ForecastBean implements Serializable {
/**
* date : 09
* high : 高温 26℃
* low : 低温 20℃
* ymd : 2022-08-09
* week : 星期二
* sunrise : 05:19
* sunset : 19:14
* aqi : 22
* fx : 北风
* fl : 2级
* type : 中雨
* notice : 记得随身携带雨伞哦
*/
private String date;
private String high;
private String low;
private String ymd;
private String week;
private String sunrise;
private String sunset;
private int aqi;
private String fx;
private String fl;
private String type;
private String notice;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getLow() {
return low;
}
public void setLow(String low) {
this.low = low;
}
public String getYmd() {
return ymd;
}
public void setYmd(String ymd) {
this.ymd = ymd;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public String getSunrise() {
return sunrise;
}
public void setSunrise(String sunrise) {
this.sunrise = sunrise;
}
public String getSunset() {
return sunset;
}
public void setSunset(String sunset) {
this.sunset = sunset;
}
public int getAqi() {
return aqi;
}
public void setAqi(int aqi) {
this.aqi = aqi;
}
public String getFx() {
return fx;
}
public void setFx(String fx) {
this.fx = fx;
}
public String getFl() {
return fl;
}
public void setFl(String fl) {
this.fl = fl;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNotice() {
return notice;
}
public void setNotice(String notice) {
this.notice = notice;
}
@Override
public String toString() {
return "ForecastBean{" +
"date='" + date + '\n' +
", high='" + high + '\n' +
", low='" + low + '\n' +
", ymd='" + ymd + '\n' +
", week='" + week + '\n' +
", sunrise='" + sunrise + '\n' +
", sunset='" + sunset + '\n' +
", aqi=" + aqi +
", fx='" + fx + '\n' +
", fl='" + fl + '\n' +
", type='" + type + '\n' +
", notice='" + notice + '\n' +
'}';
}
}
@Override
public String toString() {
return "DataBean{" +
"shidu='" + shidu + '\n' +
", pm25=" + pm25 +
", pm10=" + pm10 +
", quality='" + quality + '\n' +
", wendu='" + wendu + '\n' +
", ganmao='" + ganmao + '\n' +
", yesterday=" + yesterday.toString() +
", forecast=" + forecast.toString() +
'}';
}
}
@Override
public String toString() {
return "WeatherBean{" +
"message='" + message + '\n' +
", status=" + status +
", date='" + date + '\n' +
", time='" + time + '\n' +
", cityInfo=" + cityInfo.toString() +
", data=" + data.toString() +
'}';
}
}
5、解析数据
【1】集成json
implementation 'com.google.code.gson:gson:2.8.2'
【2】新增JsonUtil工具类
package com.tzbc.test.utils;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
/**
* Created by tzbc on 2022/8/9.
*
* @author tzbc
*/
public class JsonUtil {
private static Gson gson = new Gson();
private JsonUtil() {
}
public static <T> T fromJsonToObject(String json, Class<T> clazz) throws JsonSyntaxException {
if (TextUtils.isEmpty(json)) {
return null;
}
return gson.fromJson(json, clazz);
}
}
【3】获取WeatherBean信息
WeatherBean weatherBean = JsonUtil.fromJsonToObject(html, WeatherBean.class);
运行程序,查看log:
6、页面显示
【1】布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/tvGetWeather"
android:layout_width="180dp"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:background="@drawable/bg_button"
android:gravity="center"
android:text="Hello Weather"
android:textColor="#0F1211"
android:textSize="24dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:layout_marginRight="20dp">
<TextView
android:id="@+id/tvShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp" />
</ScrollView>
</LinearLayout>
【2】MainActivity
package com.tzbc.test.activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.tzbc.test.R;
import com.tzbc.test.bean.WeatherBean;
import com.tzbc.test.utils.HtmlService;
import com.tzbc.test.utils.JsonUtil;
/**
* ≤
* Created by tzbc on 2022/7/25.
*
* @author tzbc
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int MSG_UPDATE_WEATHER_INFO = 330;
private static final long MSG_UPDATE_WEATHER_INFO_DELAY = 200L;
private TextView mGetView;
private TextView mShowView;
private String mWeatherInfo;
private Handler mHandler = new Handler() {
@Override
public void dispatchMessage(@NonNull Message msg) {
super.dispatchMessage(msg);
if (MSG_UPDATE_WEATHER_INFO == msg.what) {
mShowView.setText(mWeatherInfo);
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mGetView = findViewById(R.id.tvGetWeather);
mGetView.setOnClickListener(this);
mShowView = findViewById(R.id.tvShow);
}
@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();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
}