安卓课程设计之小书城

对于一个学习前端的人,学校里却又要学习后端又要学习安卓是很痛苦的。

一年一度的课程设计又到来了,一脸懵逼,疯狂找资料肝课设终于给肝了出来。界面不怎么精美,实在是不想操作了。

服务器是在eclipse上写的。网络是用HttpClient实现。数据库是mysql。

数据库设计


先po上界面吧

登录界面


注册界面


首页


书籍列表


书籍详情


购物车


部分核心代码(以登录为例)

服务端代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        // TODO Auto-generated method stub  
        response.setContentType("text/html");  
        PrintWriter out = response.getWriter(); 
        
        String username = request.getParameter("username");  
        String password = request.getParameter("password");
        DBUtil.openConnection();
        String sql = "select * from user where username='"+username+"' and password='"+password+"'";
		ResultSet rs = null;
		try {
			rs = DBUtil.executSql(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String result=null; 
		try {
			while(rs.next()){
				out.print("123"); result="123";
	
			    }
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  

	    } 

客户端代码

public class GuestToServer {
    //由于Tomcat服务器部署在本地,url为本地Tomcat服务的url,IP地址为本地主机IP地址
    private   String url="http://192.168.1.105:8080/Test/LoginServlet";
    //服务器返回的结果
    String result = "";

    /**
     * 使用Post方式向服务器发送请求并返回响应
     * @param username 传递给服务器的username
     * @param password 传递给服务器的password
     * @return
     */
    public String doPost(String username, String password) throws IOException {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        //将username与password参数装入List中
        NameValuePair param1 = new BasicNameValuePair("username", username);
        NameValuePair param2 = new BasicNameValuePair("password", password);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(param1);
        params.add(param2);
        //将参数包装如HttpEntity中并放入HttpPost的请求体中
        HttpEntity httpEntity = new UrlEncodedFormEntity(params, "GBK");
        httpPost.setEntity(httpEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        //如果响应成功
        if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            //得到信息体
            HttpEntity entity = httpResponse.getEntity();
            InputStream inputStream = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String readLine = null;
            while((readLine = br.readLine()) != null){
                result += readLine;
            }
            inputStream.close();
            return result;
        }
        //响应失败
        else{
            return "Error";
        }
    }
}

github地址:https://github.com/SEWEovo/ball


猜你喜欢

转载自blog.csdn.net/weixin_41330202/article/details/80837946