Android端通过OkHttp3传递数据给服务器的servlet,存入服务器的Mysql数据库
create database cab;
use cab;
create table track(id char(4),time char(20),lon char(20),lai char(20));
<servlet>
<servlet-name>acceptServlet</servlet-name>
<servlet-class>young.acceptServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>acceptServlet</servlet-name>
<url-pattern>/accept</url-pattern>
</servlet-mapping>
public class acceptServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("utf-8");
response.setHeader("content-type", "text/html;charset=UTF-8");
//接收post数据
String t_id = new String(request.getParameter("t_id").getBytes("ISO8859-1"),"utf-8");
String t_time = new String(request.getParameter("t_time").getBytes("ISO8859-1"),"utf-8");
String t_lon = new String(request.getParameter("t_lon").getBytes("ISO8859-1"),"utf-8");
String t_lai = new String(request.getParameter("t_lai").getBytes("ISO8859-1"),"utf-8");
//向Mysql数据库写入数据
MyDataBase db=new MyDataBase();
db.insertDB(t_id,t_time,t_lon,t_lai);
db.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
insertDB()
函数,使用了PreparedStatement
代替了Statement
,在一定程度上限制了SQL注入package young;
import java.sql.*;
public class MyDataBase {
//MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost:3306/cab?useSSL=false&serverTimezone=UTC";
// 数据库的用户名与密码,需要根据自己的设置
final String USER = "root";
final String PASS = "123456";
Connection conn = null;
Statement stmt = null;
ResultSet rs =null;
PreparedStatement pst=null;
public MyDataBase() {
try {
Class.forName(JDBC_DRIVER); // 注册 JDBC 驱动
conn = DriverManager.getConnection(DB_URL,USER,PASS); // 打开链接
} catch (Exception e) {
e.printStackTrace();
}
}
public void insertDB(String t1,String t2,String t3,String t4) {
String sql = "insert into track values (?,?,?,?)";
try {
pst = conn.prepareStatement(sql);//用来执行SQL语句查询,对sql语句进行预编译处理
pst.setString(1, t1);
pst.setString(2, t2);
pst.setString(3, t3);
pst.setString(4, t4);
pst.executeUpdate();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public void close() {
// 关闭资源
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
此时,Servlet部分已经完成,我们只需要将该项目打包,部署在我们的服务器相对应位置,就可以访问该web应用,但是,我们的目的是通过Android客户端发起请求,下面我们开始Android部分的实现
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
onClick
属性方法<Button
android:id="@+id/button_start"
android:onClick="click_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始" />
click_start()
方法//点击开始按钮
public void click_start(View view){
postForm();
}
/**
* POST方式提交表单
*/
private void postForm() {
String url = " HTTP://你的服务器IP/track/accept";
OkHttpClient okHttpClient = new OkHttpClient();
//普通表单并没有指定Content-Type,这是因为FormBody继承了RequestBody,它已经指定了数据类型为application/x-www-form-urlencoded。
//表单提交
FormBody formBody = new FormBody.Builder()
.add("t_id", "test")
.add("t_time", "now")
.add("t_lon", "05")
.add("t_lai", "20")
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("提交","失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
Log.e("提交","成功");
}
}
});
}