首先,在ecplise里面新建一个叫做TestServices的web工程。在WebContent--WEB-INF--libs文件夹下导入两个jar包:mysql-connector-java-6.0.2和gson-2.3.1
mysql-connector-java-6.0.2下载链接:https://pan.baidu.com/s/1eRMByB9ERVC_T51ZBw5Q8Q 提取码: th9l
gson-2.3.1下载链接:https://pan.baidu.com/s/1NpYioUbOJTj8B1idDVupqQ 提取码: 9ir9
mysql数据库表如下:
源代码如下:
DBUtils.java:
package com.xhj.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtils {
private Connection conn;
private String url = "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC"; // 指定连接数据库的URL
private String user = "root"; // 指定连接数据库的用户名
private String password = ""; // 指定连接数据库的密码
private Statement sta;
private ResultSet rs; // 打开数据库连接
public void openConnect() {
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);// 创建数据库连接
if (conn != null) {
System.out.println("数据库连接成功"); // 连接成功的提示信息
}
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
// 获得查询user表后的数据集
public ResultSet getUser() {
// 创建 statement对象
try {
sta = conn.createStatement(); // 执行SQL查询语句
rs = sta.executeQuery("select * from user");
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
// 判断数据库中是否存在某个用户名及其密码,注册和登录的时候判断
public boolean isExistInDB(String username, String password) {
boolean isFlag = false; // 创建 statement对象
try {
System.out.println("判断用户名密码");
sta = conn.createStatement(); // 执行SQL查询语句
rs = sta.executeQuery("select * from user");// 获得结果集
if (rs != null) {
while (rs.next()) { // 遍历结果集
if (rs.getString("user_name").equals(username)) {
if (rs.getString("user_pwd").equals(password)) {
isFlag = true;
break;
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
isFlag = false;
}
return isFlag;
}
// 注册 将用户名和密码插入到数据库(id设置的是自增长的,因此不需要插入)
public boolean insertDataToDB(String username, String password) {
String sql = " insert into user ( user_name , user_pwd ) values ( " + "'" + username + "', " + "'" + password
+ "' )";
try {
sta = conn.createStatement();
// 执行SQL查询语句
return sta.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
// 关闭数据库连接
public void closeConnect() {
try {
if (rs != null) {
rs.close();
}
if (sta != null) {
sta.close();
}
if (conn != null) {
conn.close();
}
System.out.println("关闭数据库连接成功");
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
BaseBean.java:
package com.xhj.domain;
public class BaseBean {
private int code;
private String msg;
private Object data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
UserBean.java:
package com.xhj.domain;
import java.io.Serializable;
public class UserBean implements Serializable{
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
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;
}
}
LoginDateServlet.java:
package com.xhj.servlet;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.xhj.db.DBUtils;
import com.xhj.domain.BaseBean;
import com.xhj.domain.UserBean;
public class LoginDateServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("request--->"+request.getRequestURL()+"===="+request.getParameterMap().toString());
String username = request.getParameter("username"); // 获取客户端传过来的参数
String password = request.getParameter("password");
response.setContentType("text/html;charset=utf-8");
if (username == null || username.equals("") || password == null || password.equals("")) {
System.out.println("用户名或密码为空");
return;
} // 请求数据库
DBUtils dbUtils = new DBUtils();
dbUtils.openConnect();
// 打开数据库连接
BaseBean data = new BaseBean(); // 基类对象,回传给客户端的json对象
UserBean userBean = new UserBean(); // user的对象
if (dbUtils.isExistInDB(username, password)) {
// 判断账号是否存在
data.setCode(-1);
data.setData(userBean);
data.setMsg("该账号已存在");
} else if (!dbUtils.insertDataToDB(username, password)) {
// 注册成功
data.setCode(0);
data.setMsg("注册成功!!");
ResultSet rs = dbUtils.getUser();
int id = -1;
if (rs != null) {
try {
while (rs.next()) {
if (rs.getString("user_name").equals(username)
&& rs.getString("user_pwd").equals(password)) {
id = rs.getInt("user_id");
}
}
userBean.setId(id);
} catch (SQLException e) {
e.printStackTrace();
}
}
userBean.setUsername(username);
userBean.setPassword(password);
data.setData(userBean);
} else {
// 注册不成功,这里错误没有细分,都归为数据库错误
data.setCode(500);
data.setData(userBean);
data.setMsg("数据库错误");
}
Gson gson = new Gson();
String json = gson.toJson(data);
// 将对象转化成json字符串
try {
response.getWriter().println(json);
// 将json数据传给客户端
} catch (Exception e) {
e.printStackTrace();
} finally {
response.getWriter().close(); // 关闭这个流,不然会发生错误的
}
dbUtils.closeConnect(); // 关闭数据库连接}
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>TestServices</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginDateServlet</servlet-name>
<servlet-class>com.xhj.servlet.LoginDateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginDateServlet</servlet-name>
<url-pattern>/servlet/LoginDateServlet</url-pattern>
</servlet-mapping>
</web-app>
到这里,点击LoginDateServlet.java,运行一下
因为我们没有传输用户名和密码,所以输出用户名或密码为空。
可以这样测试一下,在地址后面添加?username=123&password=123
可以看到,注册成功,数据已经保存到了数据库中
接下来需要在Android Studio里面,新建一个叫做TestNet的工程
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.testnet.MainActivity">
<EditText
android:id="@+id/et_data_uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名:" />
<EditText
android:id="@+id/et_data_upass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码:"
android:inputType="number" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loginGET"
android:text="注册(GET)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loginPOST"
android:text="注册(POST)" />
</LinearLayout>
activity_main.java:
package com.example.testnet;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
String TAG = MainActivity.class.getCanonicalName();
private EditText et_data_uname;
private EditText et_data_upass;
private HashMap<String, String> stringHashMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_data_uname = (EditText) findViewById(R.id.et_data_uname);
et_data_upass = (EditText) findViewById(R.id.et_data_upass);
stringHashMap = new HashMap<>();
}
public void loginGET(View view) {
stringHashMap.put("username", et_data_uname.getText().toString());
stringHashMap.put("password", et_data_upass.getText().toString());
new Thread(getRun).start();
}
public void loginPOST(View view) {
stringHashMap.put("username", et_data_uname.getText().toString());
stringHashMap.put("password", et_data_upass.getText().toString());
new Thread(postRun).start();
}
/**
* get请求线程
*/
Runnable getRun = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
requestGet(stringHashMap);
}
};
/**
* post请求线程
*/
Runnable postRun = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
requestPost(stringHashMap);
}
};
/**
* get提交数据
*
* @param paramsMap
*/
private void requestGet(HashMap<String, String> paramsMap) {
try {
String baseUrl = "http://10.0.2.2:8080/TestServices/servlet/LoginDateServlet?";
StringBuilder tempParams = new StringBuilder();
int pos = 0;
for (String key : paramsMap.keySet()) {
if (pos > 0) {
tempParams.append("&");
}
tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8")));
pos++;
}
Log.e(TAG,"params--get-->>"+tempParams.toString());
String requestUrl = baseUrl + tempParams.toString();
// 新建一个URL对象
URL url = new URL(requestUrl);
// 打开一个HttpURLConnection连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 设置连接主机超时时间
urlConn.setConnectTimeout(5 * 1000);
//设置从主机读取数据超时
urlConn.setReadTimeout(5 * 1000);
// 设置是否使用缓存 默认是true
urlConn.setUseCaches(true);
// 设置为Post请求
urlConn.setRequestMethod("GET");
//urlConn设置请求头信息
//设置请求中的媒体类型信息。
urlConn.setRequestProperty("Content-Type", "application/json");
//设置客户端与服务连接类型
urlConn.addRequestProperty("Connection", "Keep-Alive");
// 开始连接
urlConn.connect();
// 判断请求是否成功
if (urlConn.getResponseCode() == 200) {
// 获取返回的数据
String result = streamToString(urlConn.getInputStream());
Log.e(TAG, "Get方式请求成功,result--->" + result);
} else {
Log.e(TAG, "Get方式请求失败");
}
// 关闭连接
urlConn.disconnect();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
/**
* post提交数据
*
* @param paramsMap
*/
private void requestPost(HashMap<String, String> paramsMap) {
try {
String baseUrl = "http://10.0.2.2:8080/TestServices/servlet/LoginDateServlet";
//合成参数
StringBuilder tempParams = new StringBuilder();
int pos = 0;
for (String key : paramsMap.keySet()) {
if (pos >0) {
tempParams.append("&");
}
tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8")));
pos++;
}
String params = tempParams.toString();
Log.e(TAG,"params--post-->>"+params);
// 请求的参数转换为byte数组
// byte[] postData = params.getBytes();
// 新建一个URL对象
URL url = new URL(baseUrl);
// 打开一个HttpURLConnection连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 设置连接超时时间
urlConn.setConnectTimeout(5 * 1000);
//设置从主机读取数据超时
urlConn.setReadTimeout(5 * 1000);
// Post请求必须设置允许输出 默认false
urlConn.setDoOutput(true);
//设置请求允许输入 默认是true
urlConn.setDoInput(true);
// Post请求不能使用缓存
urlConn.setUseCaches(false);
// 设置为Post请求
urlConn.setRequestMethod("POST");
//设置本次连接是否自动处理重定向
urlConn.setInstanceFollowRedirects(true);
//配置请求Content-Type
// urlConn.setRequestProperty("Content-Type", "application/json");//post请求不能设置这个
// 开始连接
urlConn.connect();
// 发送请求参数
PrintWriter dos = new PrintWriter(urlConn.getOutputStream());
dos.write(params);
dos.flush();
dos.close();
// 判断请求是否成功
if (urlConn.getResponseCode() == 200) {
// 获取返回的数据
String result = streamToString(urlConn.getInputStream());
Log.e(TAG, "Post方式请求成功,result--->" + result);
} else {
Log.e(TAG, "Post方式请求失败");
}
// 关闭连接
urlConn.disconnect();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
/**
* 将输入流转换成字符串
*
* @param is 从网络获取的输入流
* @return
*/
public String streamToString(InputStream is) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.close();
is.close();
byte[] byteArray = baos.toByteArray();
return new String(byteArray);
} catch (Exception e) {
Log.e(TAG, e.toString());
return null;
}
}
/**
* 文件下载
*
* @param fileUrl
*/
private void downloadFile(String fileUrl) {
try {
// 新建一个URL对象
URL url = new URL(fileUrl);
// 打开一个HttpURLConnection连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 设置连接主机超时时间
urlConn.setConnectTimeout(5 * 1000);
//设置从主机读取数据超时
urlConn.setReadTimeout(5 * 1000);
// 设置是否使用缓存 默认是true
urlConn.setUseCaches(true);
// 设置为Post请求
urlConn.setRequestMethod("GET");
//urlConn设置请求头信息
//设置请求中的媒体类型信息。
urlConn.setRequestProperty("Content-Type", "application/json");
//设置客户端与服务连接类型
urlConn.addRequestProperty("Connection", "Keep-Alive");
// 开始连接
urlConn.connect();
// 判断请求是否成功
if (urlConn.getResponseCode() == 200) {
String filePath = "";//下载文件保存在本地的地址
File descFile = new File(filePath);
FileOutputStream fos = new FileOutputStream(descFile);
;
byte[] buffer = new byte[1024];
int len;
InputStream inputStream = urlConn.getInputStream();
while ((len = inputStream.read(buffer)) != -1) {
// 写到本地
fos.write(buffer, 0, len);
}
} else {
Log.e(TAG, "文件下载失败");
}
// 关闭连接
urlConn.disconnect();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
/**
* 文件上传
*
* @param filePath
* @param paramsMap
*/
private void upLoadFile(String filePath, HashMap<String, String> paramsMap) {
try {
String baseUrl = "https://xxx.com/uploadFile";
File file = new File(filePath);
//新建url对象
URL url = new URL(baseUrl);
//通过HttpURLConnection对象,向网络地址发送请求
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
//设置该连接允许读取
urlConn.setDoOutput(true);
//设置该连接允许写入
urlConn.setDoInput(true);
//设置不能适用缓存
urlConn.setUseCaches(false);
//设置连接超时时间
urlConn.setConnectTimeout(5 * 1000); //设置连接超时时间
//设置读取超时时间
urlConn.setReadTimeout(5 * 1000); //读取超时
//设置连接方法post
urlConn.setRequestMethod("POST");
//设置维持长连接
urlConn.setRequestProperty("connection", "Keep-Alive");
//设置文件字符集
urlConn.setRequestProperty("Accept-Charset", "UTF-8");
//设置文件类型
urlConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + "*****");
String name = file.getName();
DataOutputStream requestStream = new DataOutputStream(urlConn.getOutputStream());
requestStream.writeBytes("--" + "*****" + "\r\n");
//发送文件参数信息
StringBuilder tempParams = new StringBuilder();
tempParams.append("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + name + "\"; ");
int pos = 0;
int size = paramsMap.size();
for (String key : paramsMap.keySet()) {
tempParams.append(String.format("%s=\"%s\"", key, paramsMap.get(key), "utf-8"));
if (pos < size - 1) {
tempParams.append("; ");
}
pos++;
}
tempParams.append("\r\n");
tempParams.append("Content-Type: application/octet-stream\r\n");
tempParams.append("\r\n");
String params = tempParams.toString();
requestStream.writeBytes(params);
//发送文件数据
FileInputStream fileInput = new FileInputStream(file);
int bytesRead;
byte[] buffer = new byte[1024];
DataInputStream in = new DataInputStream(new FileInputStream(file));
while ((bytesRead = in.read(buffer)) != -1) {
requestStream.write(buffer, 0, bytesRead);
}
requestStream.writeBytes("\r\n");
requestStream.flush();
requestStream.writeBytes("--" + "*****" + "--" + "\r\n");
requestStream.flush();
fileInput.close();
int statusCode = urlConn.getResponseCode();
if (statusCode == 200) {
// 获取返回的数据
String result = streamToString(urlConn.getInputStream());
Log.e(TAG, "上传成功,result--->" + result);
} else {
Log.e(TAG, "上传失败");
}
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
}
在TestNet--app--src--main--AndroidMainifest.xml中添加
<uses-permission android:name="android.permission.INTERNET" />
参考文章:https://blog.csdn.net/qq_34317125/article/details/80533685