不完全版天气查询

    xiaoxiao2021-12-12  9

    package com.zhidi.jsOn; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.nio.Buffer; import java.util.Scanner; import org.json.JSONException; import org.json.JSONObject; public class Tianqi { public static void main(String[] args) throws IOException, JSONException { Scanner sc = new Scanner(System.in); System.out.println("请输入查询的地点:"); String str = sc.nextLine(); Tianqi.pd(str); } public static void pd(String code) throws IOException, JSONException { switch (code) { case "北京": Tianqi.get("101010100"); break; case "郑州": Tianqi.get("101180101"); break; case "洛阳": Tianqi.get("101180901"); break; } } public static void get(String code) throws IOException, JSONException { String s1 = Tianqi.getweather("http://www.weather.com.cn/data/cityinfo/" + code + ".html"); String s2 = Tianqi.getweather("http://www.weather.com.cn/data/sk/" + code + ".html"); System.out.println(s1); System.out.println(s2); JSONObject obj = new JSONObject(s1); JSONObject obj2 = new JSONObject(s2); JSONObject o = (JSONObject) obj.get("weatherinfo"); JSONObject o2 = (JSONObject) obj2.get("weatherinfo"); System.out.println("地区:" + o.getString("city")); System.out.println("温度:" + o.getString("temp1") + "~" + o.getString("temp2")); System.out.println("天气:" + o.getString("weather")); System.out.println("风向:" + o2.getString("WD")); System.out.println("风级:" + o2.getString("WS")); } public static String getweather(String uri) throws IOException { URL url = new URL(uri); InputStream is = url.openStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8")); String str; StringBuilder sb = new StringBuilder(); while ((str = br.readLine()) != null) { sb.append(str); } br.close(); is.close(); return sb.toString(); } }
    转载请注明原文地址: https://ju.6miu.com/read-899994.html

    最新回复(0)