編輯:關於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, Listlist) { 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 Listlist = 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(); } } }
先來一張效果圖 一.ActionBar的設計首先是main.xml,先定義這些菜單,界面稍後在調整<menu xmlns:android=http://schema
acitivyt 是什麼 引用官方的說法: 是一個應用組件,用戶可與其提供的屏幕進行交互,以執行撥打電話、拍攝照片、發送電子郵件或查看地圖等操作。 每個 Activity
飛信,是中國移動推出的一款聊天工具,通過手機號注冊賬號然後跟通訊錄的朋友發短信。這個功能就是可以免費發短信,但是前提條件必須是加為了飛信好友。那麼小編就給大
每個版本的build variant代表了你可以構建的每一個版本。雖然你未直接配置build variants,你可以通過配置build type和product fla
在之前app寫完測試的時候,跑完整個老化階段包括數據收發都沒問題,鍵入