1、Android客户端、服务端、数据库开发所须要环境mysql
1.JDK8+android
2.Android studioweb
3.eclipsesql
4.mysql数据库
5.tomcatjson
2、Android客户端、服务端、数据库开发完整流程tomcat
1.用AS开发客户端,并定义连接,目前流行传输格式为JSon服务器
2.用eclipse开发服务端,并以JSon格式为传输格式网络
3.在mysql服务器中创建tableapp
4.客户端和服务端的网络链路要保证畅通
5.将服务端程序部署到192.168.1.102
6.将客户端程序安装到移动端
3、举例说明及关键代码
1.客户端
定义服务连接方式: public String serverUrl = "http://192.168.1.102:8080/androidWeb/servlet/loadMessage";
用户登陆、验证密码并取得相关信息的逻辑编码说明以下
传输参数:
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serverUrl);
List params = new ArrayList();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
调用服务并获得返回
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
httpResponse = client.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
String entityString = EntityUtils.toString(entity);
String jsonString = entityString.substring(entityString.indexOf("{"));
JSONObject json = new JSONObject(jsonString);
sendMessage(MSG_LOGIN_RESULT, json);
2.服务端
A.web.xml的定义例子:
This is the description of my J2EE component
This is the display name of my J2EE component
loadMessage
com.test.servlet.loadMessage
This is the description of my J2EE component
This is the display name of my J2EE component
NewAccount
com.test.servlet.NewAccount
loadMessage
/servlet/loadMessage
NewAccount
/servlet/NewAccount
B.DB 连接方法
Class.forName("com.mysql.jdbc.Driver");
connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8", "root", "123456
C.接口
HashMap resultMap = new HashMap();
String sql = "select * from " + DBManager.TABLE_NAME + " where " + DBManager.COLUMN_USERNAME + " = " + "'" + username + "'" ;
System.out.println("url = " + sql);
DBManager db = new DBManager();
ResultSet rst = db.query(sql);
try {
rst.next();
String pwd = rst.getString(DBManager.COLUMN_PASSWORD);
if(!password.equals(pwd)) {
resultMap.put("result_code", 1);
} else {
resultMap.put("result_code", 0);
resultMap.put(DBManager.COLUMN_USERNAME, rst.getString(DBManager.COLUMN_USERNAME));
resultMap.put(DBManager.COLUMN_GENDER, rst.getString(DBManager.COLUMN_GENDER));
resultMap.put(DBManager.COLUMN_AGE, rst.getInt(DBManager.COLUMN_AGE));
resultMap.put(DBManager.COLUMN_PHONE, rst.getString(DBManager.COLUMN_PHONE));
resultMap.put(DBManager.COLUMN_EMAIL, rst.getString(DBManager.COLUMN_EMAIL));
}
} catch (SQLException e) {
resultMap.put("result_code", 2);
e.printStackTrace();
}
return (new Gson()).toJson(resultMap);
3.DB
A.建立库表
create table login_info (
username varchar(80) not null,
password varchar (20),
gender varchar (10),
age int,
phone varchar (20),
email varchar (50),
primary key (username)
)
B.产生一条可登陆的数据(固然也能够从客户端注册来产生数据)
insert login_info values("admin","admin","nan",20,"12345678901","abc@abc.com")
4、测试结果