1. 程式人生 > >第四次團隊作業:社團申請App

第四次團隊作業:社團申請App

set 發送請求 用戶 exception true ktr java out 界面

概要:

基於上次軟件設計本著界面簡潔、易於使用的初衷,進行功能的實現,代碼位置:https://github.com/LinZezhong/testDemo

第一部分:軟件的使用

註冊:

技術分享圖片

登錄:

技術分享圖片

主界面(所有社團顯示):

技術分享圖片

點擊社團,將跳到社團申請頁面:

技術分享圖片技術分享圖片

點擊申請加入,即可提交申請

技術分享圖片

如果申請過該部門,將會提示“已申請過了"

技術分享圖片

點擊主界面”個人“,查看修改個人信息

技術分享圖片

點擊主界面”審核“,顯示自己有權限審核的社團部門

技術分享圖片

點擊相應部門,出現相應需要處理的部門申請:

技術分享圖片

點擊,

技術分享圖片

點提交,完成該申請的審核

技術分享圖片

此時,201521121076用戶點擊”我的申請“,查看自己的申請

技術分享圖片

點擊,出現自己的申請結果及通知

技術分享圖片

第二部分:代碼實現(采用http數據傳輸)

客戶端采用的是Eclipse編輯

結構如下:

技術分享圖片

服務器端被我架設在雲服務器上,使用MyEclipse+Tomcat+MySQL

MyEclipse:

技術分享圖片

思路:根據客戶端的不同功能要求連接到服務器端的不同servlet上,有servlet調用相應的MySQL操作方法獲得相應的數據,分裝成JSON數據傳輸到客戶端。

客戶端:

使用UrlConnection以post方式向服務器端發送請求。

請求方法:

public class GetPostUtil {
	public static final String urlBase="http://111.230.230.93:8080/LinkMySQL/servlet/";
	public static String sendPost(String url,String params){
		PrintWriter out = null;
		BufferedReader in = null;
		String json=null;
		try {
			URL realUrl = new URL(url);
			URLConnection conn = realUrl.openConnection();
			
			//設置通用的請求屬性
			conn.setRequestProperty("accept","*/*");
			conn.setRequestProperty("connecttion","Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1;SV1)");
			//發送post請求必須設置的兩行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			//獲取URLConnection對象對應的輸出流
			out = new PrintWriter(conn.getOutputStream());
			//發送請求參數
			out.print(params);
			//flush緩沖流的緩沖
			out.flush();
			//定義BufferdReader輸入流來讀取URL的響應
			in = new BufferedReader(
					new InputStreamReader(conn.getInputStream()));
			InputStream is = conn.getInputStream();
			json = netUtil.readString(is);	
			return json;
			
		} catch (Exception e) {
			System.out.println("發送post請求出現異常!"+e);
			e.printStackTrace();
		}
		finally{
			try{
				if(out !=null){
					out.close();
				}
				if(in != null){
					in.close();
				}
			}
			catch(IOException e){
				e.printStackTrace();
			}
		}
		return json;
	}
}

服務器回傳的數據轉化為json字符串方法:

public class netUtil {
	public static byte[] readBytes(InputStream is){
		try {
		    byte[] buffer = new byte[1024];
		    int len = -1 ;
		    ByteArrayOutputStream baos = new ByteArrayOutputStream();
		    while((len = is.read(buffer)) != -1){
		        baos.write(buffer, 0, len);
		    }
		    baos.close();
		    return baos.toByteArray();
		    } catch (Exception e) {
		        e.printStackTrace();
		    }
		    return null ;
	}
	public static String readString(InputStream is){
		return new String(readBytes(is));
	}
}

  

  

第四次團隊作業:社團申請App