Android客户端、服务端、数据库开发流程及需要环境

一、Android客户端、服务端、数据库开发所需要环境

1.JDK8+

2.Android studio

3.eclipse

4.mysql

5.tomcat

二、Android客户端、服务端、数据库开发完整流程

1.用AS开发客户端,并定义链接,目前流行传输格式为JSon

2.用eclipse开发服务端,并以JSon格式为传输格式

3.在mysql服务器中建立table

4.客户端和服务端的网络链路要保证畅通

5.将服务端程序部署到192.168.1.102

6.将客户端程序安装到移动端

三、举例说明及关键代码

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","[email protected]")

四、测试结果


你可能感兴趣的:(开发环境构筑)