Wireless ordering for Android projects (2)-client and server implementation for User Login
I. server implementation
(1) create a dynamic server project
The Code is as follows:
Package com. lc. dao; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; public class ConnectionUtil {/*** open Connection ** @ return */public static Connection open () {// 1.url//2. driver // 3. username // 4. password // configuration file: xml property file PropertiesString 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;}/*** close Connection ** @ param conn */public static void close (Connection conn) {if (conn! = Null) {try {conn. close () ;}catch (SQLException e) {e. printStackTrace ();}}}}
Package com. lc. dao;/** corresponding to the user table in the database ** Entity Class or JavaBean --- UserTabl ORM */public class User {private String username; private String password; private int id; /** construction method without parameters */public User () {super ();}/** construction method with parameters */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 interface ** defines User-related methods */public interface UserDao {// implement User logon to 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;/** used to implement the methods defined in UserDao ** interface implementation class */public class UserDaoImpl implements UserDao {@ Overridepublic User login (String username, String password) {Connection connection = ConnectionUtil. open (); String SQL = "select id, username, password from UserTbl where username =? And password =? "; Try {// pre-query PreparedStatement pstmt = connection. prepareStatement (SQL); pstmt. setString (1, username); pstmt. setString (2, password); ResultSet rs = pstmt.exe cuteQuery (); if (rs. next () {int id = rs. getInt (1); // get a user's idUser User = new user (); // set the data 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 seria LVersionUID = 1L; public LoginServlet () {super ();} protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response ); // execute dopost} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html; charset = utf8"); // sets the encoding method PrintWriter out = Response. getWriter (); // obtain the login request information String username = request. getParameter ("username"); String password = request. getParameter ("password"); // print out the test: 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 ("no user you want, login failed! "); Out. println (" no user you want, login failed! ");} Out. flush (); out. close ();}}
Ii. Client implementation
Layout file:
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; @ Overrideprotected 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 () {@ Overridepublic void onClick (View v) {String url = "http: // 172.23.252.89: 8080/WiressOrderServer/LoginServlet "; // execute the asynchronous task new mytask(cmd.exe cute (url) ;}});} String doLogin (String url) {String username = usernameEditText. getText (). toString (); String password = passwordEditText. getText (). toString (); // 1. apache clientList
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;} // multithreading: hander, Asynctaskclass MyTask extends AsyncTask
{@ Overrideprotected String doInBackground (String... params) {String url = params [0]; String result = doLogin (url); return result ;}@ Overrideprotected void onPostExecute (String result) {super. onPostExecute (result); // 1. save Toast. makeText (getApplicationContext (), result, Toast. LENGTH_SHORT ). show ();}}}