免費天氣API,天氣JSON API,不限次數獲取

聲明該獲取天API方式 轉自https://www.sojson.com/blog/305.html

實現方式結合代碼實現做進一步處理

  1. 前端頁面JS處理
  2. 後臺API處理

JS處理:

var cityId="101020100";
	        $.getJSON("http://t.weather.sojson.com/api/weather/city/"+cityId,                 
  function(data){
	        	var obj = eval("(" + JSON.stringify(data.data) + ")");
	        	console.log(obj);        	
	        	$("#tempData").html(obj.wendu+"℃");//溫度
				$("#shidu").html(obj.shidu);//溼度
				$("#tempQuality").html(obj.quality);//空氣質量
				$("#weaData").html(obj.forecast[0].type);//天氣
				$("#windData").html(obj.forecast[0].fl);//風級
     }); 

後臺處理:

public class weatherCl extends BaseCl {

    private static final String FUNC_REMIND_PROJECTLIST = null;

    @ResponseBody
    @RequestMapping(value = "/getWeatherData", method = RequestMethod.GET)
    public ResultMsg getWeatherData(String flag, String cityCode, HttpServletRequest request) {
        final Map<String, Object> entry = new HashMap<String, Object>();
        String b = null;
        try {
            b = sendGet("http://t.weather.sojson.com/api/weather/city/" + cityCode);
            System.out.println(b);
            entry.put("list", b);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ResultDomain.resultMSG(entry, ResultCodeType.SUCCESS_1,
            LogType.MODULE_SUB_CONTRACTORS.log(FUNC_REMIND_PROJECTLIST, ResultCodeType.SUCCESS_1, null));
    }

    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url;
            URL realUrl = new URL(urlNameString);
            // 打開和URL之間的連接
            URLConnection connection = realUrl.openConnection();
            // 設置通用的請求屬性
            // connection.setRequestProperty("accept", "*/*");
            // connection.setRequestProperty("connection", "Keep-Alive");
            // connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // // 建立實際的連接
            // connection.connect();
            // 獲取所有響應頭字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍歷所有的響應頭字段
            // for (String key : map.keySet()) {
            // System.out.println(key + "--->" + map.get(key));
            // }
            // 定義 BufferedReader輸入流來讀取URL的響應
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("發送GET請求出現異常!" + e);
            e.printStackTrace();
        }
        // 使用finally塊來關閉輸入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 
     * @Title: main
     * @Description: Test
     */
    public static void main(String[] args) {
        String b = sendGet("http://t.weather.sojson.com/api/weather/city/101020100");
        System.out.println(b);
    }

每次寫的很少,讀懂代碼就很簡單

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章