教你一步一步做无线点餐项目(登录篇)

2024-06-06 03:58

本文主要是介绍教你一步一步做无线点餐项目(登录篇),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文链接: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

这篇关于教你一步一步做无线点餐项目(登录篇)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035031

相关文章

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

全屋WiFi 7无死角! 华硕 RP-BE58无线信号放大器体验测评

《全屋WiFi7无死角!华硕RP-BE58无线信号放大器体验测评》家里网络总是有很多死角没有网,我决定入手一台支持Mesh组网的WiFi7路由系统以彻底解决网络覆盖问题,最终选择了一款功能非常... 自2023年WiFi 7技术标准(IEEE 802.11be)正式落地以来,这项第七代无线网络技术就以超高速

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

Java中的登录技术保姆级详细教程

《Java中的登录技术保姆级详细教程》:本文主要介绍Java中登录技术保姆级详细教程的相关资料,在Java中我们可以使用各种技术和框架来实现这些功能,文中通过代码介绍的非常详细,需要的朋友可以参考... 目录1.登录思路2.登录标记1.会话技术2.会话跟踪1.Cookie技术2.Session技术3.令牌技

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

eclipse如何运行springboot项目

《eclipse如何运行springboot项目》:本文主要介绍eclipse如何运行springboot项目问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目js录当在eclipse启动spring boot项目时出现问题解决办法1.通过cmd命令行2.在ecl