Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android項目之無線點餐(2)--用戶登錄的客戶端和服務器端實現

Android項目之無線點餐(2)--用戶登錄的客戶端和服務器端實現

編輯:關於Android編程

一、服務器端實現

(1)創建動態服務器項目

\

個部分代碼如下:

package com.lc.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionUtil {

	/**
	 * 打開連接
	 * 
	 * @return
	 */
	public static Connection open() {
		// 1.url
		// 2.driver
		// 3.username
		// 4.password
		// 配置文件xml 屬性文件Properties

		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/wiressorder?useUnicode=true&characterEncoding=gbk";
		String username = "xuuu";
		String password = "1234567890";

		try {

			Class.forName(driver);
			return DriverManager.getConnection(url, username, password);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 關閉連接
	 * 
	 * @param conn
	 */
	public static void close(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

package com.lc.dao;

/*
 * 對應數據庫中的user表
 * 
 * Entity Class或者是JavaBean---UserTabl ORM
 */
public class User {
	private String username;
	private String password;
	private int id;

	/*
	 * 無參的構造方法
	 */
	public User() {
		super();
	}

	/*
	 * 有參的構造方法
	 */

	public User(String username, String password, int id) {
		super();
		this.username = username;
		this.password = password;
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

package com.lc.dao;

/*
 * UserDao接口
 * 
 * 定義於User有關的方法
 */
public interface UserDao {
	// 實現用戶登錄
	public User login(String username, String password);
}
package com.lc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*
 * 用於實現UserDao中定義的方法
 * 
 * 接口的實現類
 */
public class UserDaoImpl implements UserDao {

	@Override
	public User login(String username, String password) {
		Connection connection = ConnectionUtil.open();
		String sql = "select id,username,password from UserTbl where username=? and password=?";
		try {
			// 預查尋
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, username);
			pstmt.setString(2, password);

			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				int id = rs.getInt(1); // 獲得一個用戶的id
				User user = new User();
				// 設置數據
				user.setId(id);
				user.setUsername(username);
				user.setPassword(password);

				return user;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			ConnectionUtil.close(connection);
		}

		return null;
	}
}
package com.lc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lc.dao.User;
import com.lc.dao.UserDao;
import com.lc.dao.UserDaoImpl;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public LoginServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response); // 都執行dopost
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf8"); // 設置編碼方式
		PrintWriter out = response.getWriter();

		// 獲得登錄的請求信息
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		// 打印出來測試:http://localhost:8080/WiressOrderServer/LoginServlet?username=tom&password=123
		// System.out.println("username:" + username + "password:" + password);

		UserDao userDao = new UserDaoImpl();
		User user = userDao.login(username, password);
		if (user != null) {
			System.out.println("username:" + user.getUsername() + "password:"+ user.getPassword());
			out.println("username:" + user.getUsername() + "password:"+ user.getPassword());
		} else {
			System.out.println("沒有你所要的用戶,登錄失敗!");
			out.println("沒有你所要的用戶,登錄失敗!");

		}
		out.flush();
		out.close();
	}

}

二、客戶端實現

\

布局文件:




    

        

        

    

    

        

        

            
        
    

    



package com.xuliugen.wiressorderclient;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpUtil {

	public static String doPost(String url, List list) {
		HttpPost post = new HttpPost(url);
		HttpEntity entity = null;
		if (list != null) {
			try {
				entity = new UrlEncodedFormEntity(list, "gbk");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			post.setEntity(entity);
		}

		HttpClient client = new DefaultHttpClient();
		try {
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				String result = EntityUtils.toString(response.getEntity());
				// save SharedPre...
				result = new String(result.getBytes("iso-8859-1"), "gbk");
				System.out.println(result);
				return result;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

package com.xuliugen.wiressorderclient;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	private EditText usernameEditText, passwordEditText;
	private Button login_button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		usernameEditText = (EditText) this.findViewById(R.id.ed_username);
		passwordEditText = (EditText) this.findViewById(R.id.ed_password);
		login_button = (Button) this.findViewById(R.id.login_button);

		login_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String url = "http://172.23.252.89:8080/WiressOrderServer/LoginServlet";
				// 執行異步任務
				new MyTask().execute(url);
			}
		});
	}

	String doLogin(String url) {

		String username = usernameEditText.getText().toString();
		String password = passwordEditText.getText().toString();

		// 1.apache client
		List list = new ArrayList();
		NameValuePair p1 = new BasicNameValuePair("username", username);
		NameValuePair p2 = new BasicNameValuePair("password", password);
		list.add(p1);
		list.add(p2);

		String msg = HttpUtil.doPost(url, list);

		return msg;

	}

	// 多線程的使用:hander、Asynctask
	class MyTask extends AsyncTask {

		@Override
		protected String doInBackground(String... params) {
			String url = params[0];
			String result = doLogin(url);
			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			// 1.保存信息
			Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT)
					.show();
		}

	}
}




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved