請求 `https://wthrcdn.etouch.cn/weather_mini?citykey=101010100`,解析 JSON 顯示天氣。
// 網絡請求 (需在子線程)
String urlStr = "https://wthrcdn.etouch.cn/weather_mini?citykey=101010100";
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
InputStream is = conn.getInputStream();
String json = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining());
// JSON 解析 (org.json)
JSONObject root = new JSONObject(json);
int status = root.getInt("status");
if (status == 1000) {
JSONObject data = root.getJSONObject("data");
String city = data.getString("city");
JSONArray forecast = data.getJSONArray("forecast");
for (int i=0; i
推薦使用 **OkHttp** + **Gson/Moshi** 或 **Retrofit**,代碼更簡潔、支持取消、連接池、自動解析。