閱讀512 返回首頁    go 技術社區[雲棲]


天氣預報進展

昨天下午和今天早上重新對程序進行了一下修改,增加了如下功能:

一、首次運行時會先暫停五秒,然後每24小時運行一次

二、每次運行程序的時候會先清空一次數據庫,然後再執行抓取、解析、插入的工作

三、增加了程序30%的注釋

四、部分類和主函數之間麵向對象的實現得到了改善

五、發布了JSP的WEB頁麵,WEB頁麵有兩個:Index和detail。Index主要顯示數據庫city_info中的數據項信息,detail主要通過頁麵傳參的形式通過city_info及weather_info兩張表的聯合查詢,顯示出Index頁麵鼠標點擊城市的對應天氣信息。每個城市都有自己的id值,這個id值就是對外發布出去的HTTP接口,頁麵運行結果如以下幾個頁麵的地址所示:

 https://localhost:8080/WeatherWeb/ShowDetail.jsp?id=101050301

{"weatherinfo":{"city":"牡丹江","date":2011年12月1日","year":辛卯年","week":星期四","description":天氣寒冷,建議著厚羽絨服、毛皮大衣加厚毛衣等隆冬服裝。年老體弱者尤其要注意保暖防凍。","temp1":"-7℃~-18℃","winddricetion1":"西風3-4級轉微風","windpower1":"3-4級轉小於3級 ","temp2":"-6℃~-15℃","winddricetion2":"西北風3-4級轉微風","windpower2":"3-4級轉小於3級 ","temp3":"-6℃~-15℃","winddricetion3":"西南風3-4級轉微風","windpower3":"3-4級轉小於3級 ","temp4":"-6℃~-16℃","winddricetion4":"西南風3-4級轉微風","windpower4":"3-4級轉小於3級 ","temp5":"-6℃~-17℃","winddricetion5":"西南風3-4級轉微風","windpower5":"3-4級轉小於3級"}}


https://localhost:8080/WeatherWeb/ShowDetail.jsp?id=101060301

{"weatherinfo":{"city":"延吉","date":2011年12月1日","year":辛卯年","week":星期四","description":天氣寒冷,建議著厚羽絨服、毛皮大衣加厚毛衣等隆冬服裝。年老體弱者尤其要注意保暖防凍。","temp1":"-6℃~-17℃","winddricetion1":"西風轉東風小於3級","windpower1":"小於3級 ","temp2":"-4℃~-12℃","winddricetion2":"東風轉西風小於3級","windpower2":"小於3級 ","temp3":"-4℃~-12℃","winddricetion3":"西風3-4級轉小於3級","windpower3":"3-4級轉小於3級 ","temp4":"-2℃~-13℃","winddricetion4":"西風小於3級","windpower4":"小於3級 ","temp5":"-3℃~-11℃","winddricetion5":"西風小於3級","windpower5":"小於3級"}}


https://localhost:8080/WeatherWeb/ShowDetail.jsp?id=101081000

{"weatherinfo":{"city":"唿倫貝爾","date":2011年12月1日","year":辛卯年","week":星期四","description":天氣寒冷,建議著厚羽絨服、毛皮大衣加厚毛衣等隆冬服裝。年老體弱者尤其要注意保暖防凍。","temp1":"-17℃~-26℃","winddricetion1":"微風","windpower1":"小於3級 ","temp2":"-11℃~-20℃","winddricetion2":"微風","windpower2":"小於3級 ","temp3":"-12℃~-25℃","winddricetion3":"微風","windpower3":"小於3級 ","temp4":"-7℃~-29℃","winddricetion4":"微風","windpower4":"小於3級 ","temp5":"-9℃~-28℃","winddricetion5":"微風","windpower5":"小於3級"}}



六、將數據庫中解析出的字段通過拚字符串的方法重新轉化為JSON格式對外提供接口

 

 

需要改善增加的地方有:

一、程序麵向對象的體現不是很好

二、增加曆史表,每天自動運行程序時不是使用truncate清空表,而是將曆史記錄轉移至曆史表中

三、定時器,改造為每天,比如零點或者六點自動運行

四、放棄JSP,改用SERVLET的方法重新製作WEB端,需要學習WEB開發!

 

最近的事情也要做個規劃:

一、今天周四了,打算利用這周四、五、六、日的業餘時間學習製作一下馬士兵的BBS項目,然後自己動手改造做一個自己的項目。旨在熟悉WEB開發,前台後台管理員、登陸等等。畢竟以前從未做過WEB開發。

二、 下周利用時間學習製作下馬士兵的網上商城項目,然後自己動手也跟著學習製作一個自己的網上商城。

三、SERVLET還是前提啊。。。。需要抓緊學習。


增加的部分代碼如下:

主函數定時器實現:

public class TestWeather {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Timer tt=new Timer();//設置定時器
		TimerTaskMine tk=new TimerTaskMine();

		 tt.schedule(tk, 1000*5, 1000*3600*24);//程序不關,每次執行先暫停五秒,以後每隔24小時運行一次

		
		
	}

}

class TimerTaskMine extends TimerTask{
	private static GetInfo get;//調用靜態方法從GetInfo類中獲取網頁信息
	@Override
	public void run() {
		
		//private static ReadJson read;//調用靜態方法從ReadJson類中讀取文件信息
		//private static InputStream inputStream;

		/* 主函數 */
	//int i = 0;
		
		//Properties可保存在流中或從流中加載。屬性列表中每個鍵及其對應值都是一個字符串
		Properties prop = new Properties();
		InputStream inputStream = new TestWeather().getClass().getResourceAsStream("/weather_code.properties");

		try {
			prop.load(inputStream);//從輸入流中讀取屬性列表
			// System.out.println(prop.getProperty("101020100"));
			Enumeration<?> en = prop.propertyNames();//返回屬性列表中所有鍵的枚舉
			String obj = "";
			// get=new GetInfo();
			// get.getInfo("101020100");
			
	        TruncateMysql.truncateMysql();//清空數據庫數據表內容
            System.out.println("truncate over!");
            
			while (en.hasMoreElements()) {//測試此枚舉是否包含更多的元素。

				obj = (String) en.nextElement();//obj即為weather_code.properties中獲得的城市代碼
				// System.out.println(obj);
    
				// get.getInfo(Integer.parseInt(obj));
				get = new GetInfo();
				// System.out.println(obj.);
				get.getInfo(obj);
				// 抓取網頁
				// get=new GetInfo();
				// get.getInfo(obj);
				// get=new GetInfo();
				// get.getInfo(Integer.parseInt(obj));

			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 關閉IO資源
			try {
				if (null != inputStream)
				{
					inputStream.close();
					inputStream = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	

		System.out.println("helloworld");
	}
	
}


清空表的實現:

public class TruncateMysql {
    
	public static void truncateMysql() {
		
		//將連接和預編譯的 SQL語句的對象初始化為0
		Statement stmt=null;
    	Connection conn=null;
    	
    	try {
    		
    		Class.forName("com.mysql.jdbc.Driver");//Load the Driver
    		
    		conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!");//Connect to the DataBase
    	   
    		stmt=conn.createStatement();
    		
    	    String sql="truncate table weather_info";
    	    stmt.executeUpdate(sql);//Execute the SQL
    	    
    	}catch (ClassNotFoundException e) {
    		e.printStackTrace();
    	}catch (SQLException e) {
    		e.printStackTrace();
    	}finally {//close
    		try {//按照順序先關閉stmt再關閉conn
    			if(stmt!=null) {
    				stmt.close();
    				stmt=null;
    			}
    		}catch (SQLException e) {
    			e.printStackTrace();
    		}
    		
    		try {
    			if(conn!=null) {
    				conn.close();
    				conn=null;
    			}
    		}catch (SQLException e) {
    			e.printStackTrace();
    		}
    	}
    }


 

JSP主頁麵實現:

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>

<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!");

Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from city_info");


%>  
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>天氣預報發布頁主頁</title>
</head>
<body>

<table border="1">
<%
while(rs.next()) {
%>
    <tr>
        <td><%= rs.getInt("id") %></td>
        <td><%= rs.getInt("city_id") %></td>
        <td><a href="ShowDetail.jsp?id=<%= rs.getInt("city_id") %>"><%= rs.getString("city_name") %></a></td> 
<%--    <td><%= rs.getString("weather_date") %></td>
        <td><%= rs.getString("weather_year") %></td>
        <td><%= rs.getString("weather_week") %></td>
        <td><%= rs.getString("weather_temp") %></td>
        <td><%= rs.getString("weather_winddriection") %></td>
        <td><%= rs.getString("weather_windpower") %></td>
        <td><%= rs.getString("weather_desprition") %></td> --%>
    </tr>
<%
}
%>
</table>
</body>

<%
rs.close();
stmt.close();
conn.close();
%>
</html>


JSP detail頁麵的實現

將內容轉化為JSON格式

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>
<%
String city_id=request.getParameter("id");
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select city_name,weather_date,weather_year,weather_week,weather_temp,weather_winddirection,weather_windpower,weather_description from city_info,weather_info where city_info.city_id=weather_info.city_id and city_info.city_id='"+city_id+"'");
%>  
<%
boolean flag = true;
int i=0;
int j=0;
int k=0;
int m=0;
while(rs.next()) {
      %>
      <%if(flag){ %>{"weatherinfo":{"city":"<%= rs.getString("city_name") %>","date":<%= rs.getString("weather_date") %>","year":<%= rs.getString("weather_year") %>","week":<%= rs.getString("weather_week") %>","description":<%= rs.getString("weather_description") %><%
      flag = false;
      } %>","temp<%++i;out.print(i);%>":"<%= rs.getString("weather_temp")%>","winddricetion<%++j;out.print(j);%>":"<%= rs.getString("weather_winddirection")%>","windpower<%++k;out.print(k);%>":"<%= rs.getString("weather_windpower")%><%}%>"}}
<%
rs.close();
stmt.close();
conn.close();
%>


 

最後更新:2017-04-02 06:52:11

  上一篇:go 本機兩個Android模擬器之間的網絡通信
  下一篇:go iPhone與iPad開發實戰——精通iOS開發--視頻