Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實現(xiàn)數(shù)據(jù)交互?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
創(chuàng)新互聯(lián)建站制作網(wǎng)站網(wǎng)頁找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁設(shè)計,成都網(wǎng)站設(shè)計、成都網(wǎng)站制作,網(wǎng)站設(shè)計,企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為成百上千服務(wù),創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣服務(wù)!
環(huán)境部署
服務(wù)器:apache-tomcat-8.5.9
語言版本:Java 1.8.0_101
編譯環(huán)境:Eclipse
android Studio
調(diào)用jar包:httpclient-4.2.5,httpcore-4.2.4 //HttpClient父類
MySQL-connector-java-5.1.40-bin //用于連接mysql數(shù)據(jù)庫
思路:涉及到服務(wù)器端mysql數(shù)據(jù)庫安裝、web應(yīng)用部分開發(fā)和Android客戶端開發(fā)三個部分
步驟:
1、mysql數(shù)據(jù)庫安裝
a、先安裝mysql-installer-community-5.7.17.0,其中在Setup Type上選擇“Server only”,然后記住數(shù)據(jù)庫端口號和賬號(例如:root)密碼(例如:123456),如下圖:
b、安裝成功驗證。命令行窗口輸入密碼,然后輸入顯示所有數(shù)據(jù)庫命令:show databases; 一定要有分號,并按回車。
c、NavicatforMySQL下載及使用。注冊,然后連接數(shù)據(jù)庫,輸入密碼后,能夠看到已存在的數(shù)據(jù)庫,可以在其中進行相關(guān)數(shù)據(jù)庫和數(shù)據(jù)表的創(chuàng)建操作。
(具體以參考資料中的內(nèi)容為主)
2、web應(yīng)用部分開發(fā)
a、新建servlet,并且配置好web.xml中的相應(yīng)信息(在WebContent下的WEB-INF文件夾下加入web.xml文件來連接servlet與jsp前端),此外還需在libs中添加mysql-connector-java-5.1.37-bin.jar文件,代碼如下:
package com.Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import com.DBTool.DBUtil; @WebServlet("/Servlet") public class Login extends HttpServlet { private static final long serialVersionUID = L; /** * @see HttpServlet#HttpServlet() */ public Login() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String ID = request.getParameter("ID"); String PW= request.getParameter("PW"); boolean type=false; response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); try { Connection con=DBUtil.getConnection(); Statement stmt=con.createStatement(); //mysql數(shù)據(jù)庫中的數(shù)據(jù)表,表名叫:demotable ,需要自己預(yù)先在數(shù)據(jù)庫中進行創(chuàng)建,包含相應(yīng)的字段和記錄。 String sql="select * from mysql.demotable where uid="+ID+" and pwd="+PW; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) { type=true; } } catch(Exception ex) { ex.printStackTrace(); } finally { DBUtil.Close(); out.print(type); out.flush(); out.close(); } } }
web.xml內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>web Login Login com.Servlet.Login Login /Login index.html index.jsp
b、前端界面設(shè)計(TestPage.jsp)如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here
c、在java Resources下的src文件夾中新建com.DBTool包,用作數(shù)據(jù)池來連接數(shù)據(jù)庫,在包中建立DBUtil類實現(xiàn)功能,代碼如下:
package com.DBTool; import java.sql.*; public class DBUtil { //其中mysql是數(shù)據(jù)庫名稱,在mysql57版本的數(shù)據(jù)庫中已經(jīng)預(yù)先新建完成;3306是mysql數(shù)據(jù)庫的端口號。 private static String url="jdbc:mysql://localhost:3306/mysql"; //com.mysql.jdbc.Driver是mysql-connector-java-5.1.40中的驅(qū)動包路徑 private static String driverClass="com.mysql.jdbc.Driver"; //mysql的賬號和密碼是在安裝mysql中進行設(shè)置的,這里拿來用即可。 private static String username="root"; private static String password="123456"; private static Connection conn; //裝載驅(qū)動 static{ try{ Class.forName(driverClass); } catch(ClassNotFoundException e){ e.printStackTrace(); } } //獲取數(shù)據(jù)庫連接 public static Connection getConnection(){ try{ conn=DriverManager.getConnection(url,username,password); } catch(SQLException e){ e.printStackTrace(); } return conn; } //建立數(shù)據(jù)庫連接 public static void main(String[] args){ Connection conn=DBUtil.getConnection(); if(conn!=null){ System.out.println("數(shù)據(jù)庫連接成功"); } else{ System.out.println("數(shù)據(jù)庫連接失敗"); } } //關(guān)閉數(shù)據(jù)庫連接 public static void Close(){ if(conn!=null){ try{ conn.close(); } catch(SQLException e){ e.printStackTrace(); } } } }
d、運行服務(wù)器,測試是否成功搭建。
3、Android部分開發(fā)
僅附上核心部分代碼,如下:
public void SendByHttpClient(final String id, final String pw){ new Thread(new Runnable() { @Override public void run() { try { HttpClient httpclient=new DefaultHttpClient(); HttpPost httpPost=new HttpPost("http://web應(yīng)用部署服務(wù)器上的IP地址:/HttpClientDemo/Login");//服務(wù)器地址,指向Servlet Listparams=new ArrayList ();//將id和pw裝入list params.add(new BasicNameValuePair("ID",id)); params.add(new BasicNameValuePair("PW",pw)); final UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,"utf-8");//以UTF-8格式發(fā)送 httpPost.setEntity(entity); HttpResponse httpResponse= httpclient.execute(httpPost); if(httpResponse.getStatusLine().getStatusCode()==200)//在200毫秒之內(nèi)接收到返回值 { HttpEntity entity=httpResponse.getEntity(); String response=EntityUtils.toString(entity1, "utf-8");//以UTF-8格式解析 Message message=new Message(); message.what=USER_LOGIN; message.obj=response; handler.sendMessage(message);使用Message傳遞消息給線程 } } catch (Exception e) { e.printStackTrace(); } } }).start(); }
看完上述內(nèi)容,你們掌握Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實現(xiàn)數(shù)據(jù)交互的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!