原文鏈接:http://www.eoeandroid.com/thread-189017-1-1.html
項目名稱:無線點餐系統客戶端准備工具:Eclipse4.2(我是4.2的,你們可以3.5,3.6都沒問題),ADT20,SDK20
服務端准備工具:Myeclipse,MySQL以及Navicat 8 for MSQL和Tomcat
項目需求:傳統的餐飲行業,一般都是餐廳服務員人工完成的,過程為顧客進入餐廳坐下后,服務員點菜,然后菜單交給廚師,廚師開始做菜,這過程在小飯館可以,大飯館就不行了。所以為了解決這個問題,特此推出了無線點餐系統,由無線路由器和服務器組成。
系統結構:Android通過無線網絡訪問后台服務器,技術:客戶端采用java,web采用servlet,通信使用Tomcat
系統功能:登錄,主菜單,點餐,結算,查台,更新,並,轉台
客戶端部分:
那今天講講登錄功能,為提高安全性,登錄是通過網絡,在后台通過數據庫將用戶名密碼進行查詢,如匹配可以進入主菜單,不符合的告示用戶名和密碼錯誤。我光是登錄就弄了一晚上。首先是在客戶端創建android項目,名叫如WirelesOrder_Client,接着創建三個包:com.amaker.wlo存放各個Activity;provider存放本地數組庫,util存放工具包,那布局創建一個login的,外層是線性布局,里面套個表格布局,具體如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/login" android:orientation="vertical" > <TableLayout android:layout_marginTop="130dip" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:stretchColumns="1" > <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用戶名:" android:textColor="#0000ff" /> <EditText android:id="@+id/usernameEdit" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密碼:" android:textColor="#0000ff" /> <EditText android:id="@+id/passEdit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:password="true" /> </TableRow> <TableRow> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陸" /> </TableRow> </TableLayout> </LinearLayout>
其次在activity聲明各個組建以及findviewbyid尋找ID。下面開始就是重點了,實現后台數據庫訪問,方法很多,我們采用Http請求HttpResquest和HttpRsponse對象,以及發送get和post返回信息:
package com.amaker.util; public class HttpUtil { // 基礎URL public static final String BASE_URL="http://這里是你的IP地址:端口號/服務器項目名/"; // 獲得Get請求對象request public static HttpGet getHttpGet(String url){ HttpGet request = new HttpGet(url); return request; } // 獲得Post請求對象request public static HttpPost getHttpPost(String url){ HttpPost request = new HttpPost(url); return request; } // 根據請求獲得響應對象response public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException{ HttpResponse response = new DefaultHttpClient().execute(request); return response; } // 根據請求獲得響應對象response public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException{ HttpResponse response = new DefaultHttpClient().execute(request); return response; } // 發送Post請求,獲得響應查詢結果 public static String queryStringForPost(String url){ // 根據url獲得HttpPost對象 HttpPost request = HttpUtil.getHttpPost(url); String result = null; try { // 獲得響應對象 HttpResponse response = HttpUtil.getHttpResponse(request); // 判斷是否請求成功 if(response.getStatusLine().getStatusCode()==200){ // 獲得響應 result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "網絡異常!"; return result; } catch (IOException e) { e.printStackTrace(); result = "網絡異常!"; return result; } return null; } // 獲得響應查詢結果 public static String queryStringForPost(HttpPost request){ String result = null; try { // 獲得響應對象 HttpResponse response = HttpUtil.getHttpResponse(request); // 判斷是否請求成功 if(response.getStatusLine().getStatusCode()==200){ // 獲得響應 result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "網絡異常!"; return result; } catch (IOException e) { e.printStackTrace(); result = "網絡異常!"; return result; } return null; } // 發送Get請求,獲得響應查詢結果 public static String queryStringForGet(String url){ // 獲得HttpGet對象 HttpGet request = HttpUtil.getHttpGet(url); String result = null; try { // 獲得響應對象 HttpResponse response = HttpUtil.getHttpResponse(request); // 判斷是否請求成功 if(response.getStatusLine().getStatusCode()==200){ // 獲得響應 result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "網絡異常!"; return result; } catch (IOException e) { e.printStackTrace(); result = "網絡異常!"; return result; } return null; }
就這個在HttpPost獲取對象始終是空指針。后面復制人家的居然好了- -。這不是在坑我嘛,就是URL地址你改成你自己的IP地址就行了。下面在登錄功能定義幾個方法:
showDialog()方法:用於顯示對話框
// 創建一個對話框 private void ShowDialog(String msg) { AlertDialog.Builder bulider = new AlertDialog.Builder(this); bulider.setMessage(msg); bulider.setCancelable(false); bulider.setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).show(); }
validate方法:驗證用戶名和密碼是否正確
// 驗證用戶是否正確 private boolean vaildate() { String username = user.getText().toString(); if (username.equals("")) { ShowDialog("用戶必須填"); return false; } String pwd = passwords.getText().toString(); if (pwd.equals("")) { ShowDialog("密碼必須填"); return false; } return true; }
query:發送post請求,獲取響應結果,通過用戶名和密碼進行查詢
private String query(String username, String password) { //查詢字符串 String queryString = "username=" + username +"&password="+password; //查詢URL String url = HttpUtil.BASE_URL + "/servlet/LoginServlet?" + queryString; //返回結果 return HttpUtil.queryStringForPost(url); }
saveUserMsg:將查詢結果保存到xml配置文件里,以便在后面的點餐中使用用戶信息,login方法可以調用saveUsermsg方法:
rivate void saveUserMsg(String msg) { String id = ""; // 用戶名稱 String name = ""; // 獲得信息數組 String[] msgs = msg.split(";"); int idx = msgs[0].indexOf("="); id = msgs[0].substring(idx+1); idx = msgs[1].indexOf("="); System.out.println("idx-----"+idx); name = msgs[1].substring(idx+1); // 共享信息 SharedPreferences pre = getSharedPreferences("user_msg", MODE_WORLD_WRITEABLE); SharedPreferences.Editor editor = pre.edit(); editor.putString("id", id); editor.putString("name", name); editor.commit(); } //登錄方法 private boolean login() { String username = user.getText().toString(); String password = passwords.getText().toString(); String result = query(username, password); if (result!=null&&result.equals("0")) { return false; }else { saveUserMsg(result); return true; }
在登錄按鈕加監聽,先調用validate驗證再通過login,如成功進入主菜單,反之提示登錄失敗信息
logining.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (vaildate()) { if (login()) { Intent intent=new Intent(LoginActivity.this,MainAcitivity.class); startActivity(intent); }else { ShowDialog("用戶名或者密碼輸入錯誤,請重新輸入"); } } } });
后續繼續更新。

特別推薦:
漂亮的快捷撥打電話的widget程序
http://www.eoeandroid.com/thread-176096-1-1.html
二級下拉菜單+快速搜索
http://www.eoeandroid.com/thread-163892-1-1.html
關於第三方控件ViewFlow的用法總結
http://www.eoeandroid.com/thread-157603-1-1.html
