Interface retrieval is completed in two steps

Step 1: Introduce the JAR package

  1. Online jars
  2. If online JAR package is invalid, private message me.

Step 2: Create the class implementation

  1. Create a JAVA class in a JAVA project (mine: WeatherUtil)
  2. Change the cityID in the class to the region ID you need to display. Mine is Shanghai.

WeatherUtil class code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

/** * Java call central weather bureau weather forecast interface **@author Administrator
 *
 */
public class WeatherUtil {

    /** * get the weather <br> * method name: getWeekWeatherMap <br> *@paramCityid Cityid */
    public static List<Map<String, Object>> getWeekWeatherMap(String Cityid)
            throws IOException, NullPointerException {
        // API connected to the Central Meteorological Observatory
        URL url = new URL("http://m.weather.com.cn/data/" + Cityid + ".html");
        URLConnection connectionData = url.openConnection();
        connectionData.setConnectTimeout(1000);
        // Get weather conditions for 1 to 6 days
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    connectionData.getInputStream(), "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = br.readLine()) ! =null)
                sb.append(line);
            String datas = sb.toString();
            System.out.println(datas);
            JSONObject jsonData = JSONObject.fromObject(datas);
            JSONObject info = jsonData.getJSONObject("weatherinfo");
            for (int i = 1; i <= 6; i++) {
                // Get the date for the next 6 days
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DAY_OF_YEAR, i - 1);
                Date date = cal.getTime();
                SimpleDateFormat sf = new SimpleDateFormat("Yyyy year MM month DD day");
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("city", info.getString("city").toString());/ / the city
                map.put("date_y", sf.format(date));/ / date
                map.put("week", getWeek(cal.get(Calendar.DAY_OF_WEEK)));/ / week
                map.put("fchh", info.getString("fchh").toString());// Release time
                map.put("weather", info.getString("weather" + i).toString());/ / the weather
                map.put("temp", info.getString("temp" + i).toString());/ / temperature
                map.put("wind", info.getString("wind" + i).toString());/ / the wind
                map.put("fl", info.getString("fl" + i).toString());/ / the wind speed
                // map.put("index", info.getString("index").toString()); //
                // Today's dressing index
                // map.put("index_uv", info.getString("index_uv").toString()); //
                // Uv index
                // map.put("index_tr", info.getString("index_tr").toString()); //
                // Tourism index
                // map.put("index_co", info.getString("index_co").toString()); //
                // Comfort index
                // map.put("index_cl", info.getString("index_cl").toString()); //
                // Morning exercise index
                // map.put("index_xc", info.getString("index_xc").toString()); //
                // Car Wash index
                // map.put("index_d", info.getString("index_d").toString()); //
                // Weather detail dressing indexlist.add(map); }}catch (SocketTimeoutException e) {
            System.out.println("Connection timed out");
        } catch (FileNotFoundException e) {
            System.out.println("Error loading file");
        }

        return list;

    }

    /** ** get real-time weather 1<br> * getTodayWeather <br> **@paramCityid * City code */
    public static Map<String, Object> getTodayWeather1(String Cityid)
            throws IOException, NullPointerException {
        // API connected to the Central Meteorological Observatory
        URL url = new URL("http://www.weather.com.cn/data/sk/" + Cityid
                + ".html");
        URLConnection connectionData = url.openConnection();
        connectionData.setConnectTimeout(1000);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    connectionData.getInputStream(), "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = br.readLine()) ! =null)
                sb.append(line);
            String datas = sb.toString();
            System.out.println(datas);
            JSONObject jsonData = JSONObject.fromObject(datas);
            JSONObject info = jsonData.getJSONObject("weatherinfo");
            map.put("city", info.getString("city").toString());/ / the city
            map.put("temp", info.getString("temp").toString());/ / temperature
            map.put("WD", info.getString("WD").toString());/ / the wind
            map.put("WS", info.getString("WS").toString());/ / the wind speed
            map.put("SD", info.getString("SD").toString());/ / humidity
            map.put("time", info.getString("time").toString());// Release time

        } catch (SocketTimeoutException e) {
            System.out.println("Connection timed out");
        } catch (FileNotFoundException e) {
            System.out.println("Error loading file");
        }

        return map;

    }


    /** ** get real-time weather 2<br> * getTodayWeather <br> **@paramCityid * City code */
    public static Map<String, Object> getTodayWeather2(String Cityid)
            throws IOException, NullPointerException {
        // API connected to the Central Meteorological Observatory
        URL url = new URL("http://www.weather.com.cn/data/cityinfo/" + Cityid
                + ".html");
        URLConnection connectionData = url.openConnection();
        connectionData.setConnectTimeout(1000);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    connectionData.getInputStream(), "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = br.readLine()) ! =null)
                sb.append(line);
            String datas = sb.toString();
            System.out.println(datas);
            JSONObject jsonData = JSONObject.fromObject(datas);
            JSONObject info = jsonData.getJSONObject("weatherinfo");
            map.put("city", info.getString("city").toString());/ / the city
            map.put("temp1", info.getString("temp1").toString());// Maximum temperature
            map.put("temp2", info.getString("temp2").toString());// Minimum temperature
            map.put("weather", info.getString("weather").toString());/ / the weather
            map.put("ptime", info.getString("ptime").toString());// Release time

        } catch (SocketTimeoutException e) {
            System.out.println("Connection timed out");
        } catch (FileNotFoundException e) {
            System.out.println("Error loading file");
        }

        return map;

    }

    private static String getWeek(int iw) {
        String weekStr = "";
        switch (iw) {
            case 1:
                weekStr = "Sunday";
                break;
            case 2:
                weekStr = "Monday";
                break;
            case 3:
                weekStr = "Tuesday";
                break;
            case 4:
                weekStr = "Wednesday";
                break;
            case 5:
                weekStr = "Thursday";
                break;
            case 6:
                weekStr = "Friday";
                break;
            case 7:
                weekStr = "Saturday";
                break;
            default:
                break;
        }
        return weekStr;
    }

    public static void main(String[] args) {
        try {
            // Test to get real-time weather 1(including wind condition, humidity)
            Map<String, Object> map = getTodayWeather1("101020100");
            System.out.println(map.get("city") + "\t" + map.get("temp")
                    + "\t" + map.get("WD") + "\t" + map.get("WS")
                    + "\t" + map.get("SD") + "\t" + map.get("time"));

            // Test to get real-time weather 2(including weather, temperature range)
            Map<String, Object> map2 = getTodayWeather2("101010100");
            System.out.println(map2.get("city") + "\t" + map2.get("temp1")
                    + "\t" + map2.get("temp2") + "\t" + map2.get("weather")
                    + "\t" + map2.get("ptime"));

            // Test to get a week's weather
            List<Map<String, Object>> listData = getWeekWeatherMap("101010100");
            for (int j = 0; j < listData.size(); j++) {
                Map<String, Object> wMap = listData.get(j);
                System.out.println(wMap.get("city") + "\t" + wMap.get("date_y")
                        + "\t" + wMap.get("week") + "\t" + wMap.get("weather")
                        + "\t" + wMap.get("temp") + "\t" + wMap.get("wind")
                        + "\t" + wMap.get("fl")); }}catch(Exception e) { e.printStackTrace(); }}}Copy the code