Android應(yīng)用中怎么與ApacheTomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互

Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

創(chuàng)新互聯(lián)建站制作網(wǎng)站網(wǎng)頁(yè)找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁(yè)設(shè)計(jì),成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,網(wǎng)站設(shè)計(jì),企業(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ò)營(yíng)銷推廣服務(wù)!

環(huán)境部署

服務(wù)器:apache-tomcat-8.5.9

語(yǔ)言版本: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ù)庫(kù)

思路:涉及到服務(wù)器端mysql數(shù)據(jù)庫(kù)安裝、web應(yīng)用部分開發(fā)和Android客戶端開發(fā)三個(gè)部分

步驟:

1、mysql數(shù)據(jù)庫(kù)安裝

a、先安裝mysql-installer-community-5.7.17.0,其中在Setup Type上選擇“Server only”,然后記住數(shù)據(jù)庫(kù)端口號(hào)和賬號(hào)(例如:root)密碼(例如:123456),如下圖:

Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互

Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互

b、安裝成功驗(yàn)證。命令行窗口輸入密碼,然后輸入顯示所有數(shù)據(jù)庫(kù)命令:show databases; 一定要有分號(hào),并按回車。

Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互

Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互

c、NavicatforMySQL下載及使用。注冊(cè),然后連接數(shù)據(jù)庫(kù),輸入密碼后,能夠看到已存在的數(shù)據(jù)庫(kù),可以在其中進(jìn)行相關(guān)數(shù)據(jù)庫(kù)和數(shù)據(jù)表的創(chuàng)建操作。

Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互

Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互

(具體以參考資料中的內(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ù)庫(kù)中的數(shù)據(jù)表,表名叫:demotable ,需要自己預(yù)先在數(shù)據(jù)庫(kù)中進(jìn)行創(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)容如下:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <display-name>web</display-name>
   <servlet>
     <display-name>Login</display-name>
     <servlet-name>Login</servlet-name>
     <servlet-class>com.Servlet.Login</servlet-class>
   </servlet>
   <servlet-mapping>
     <servlet-name>Login</servlet-name>
     <url-pattern>/Login</url-pattern>
   </servlet-mapping>
   <welcome-file-list>
     <welcome-file>index.html</welcome-file>
     <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
 </web-app>

b、前端界面設(shè)計(jì)(TestPage.jsp)如下:

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
 <form id="from" action="Login" method="post">
 <table>
 <tr><td>用戶名</td><td><input type="text" name="ID"></td></tr>
 <tr><td>密碼</td><td><input type="password" name="PW"></td></tr>
 <tr><td colspan="2" align="center"><input type="submit" value="登陸"/></td></tr>
 </table>
 </form>
 </body>
 </html>

c、在java Resources下的src文件夾中新建com.DBTool包,用作數(shù)據(jù)池來連接數(shù)據(jù)庫(kù),在包中建立DBUtil類實(shí)現(xiàn)功能,代碼如下:

package com.DBTool;
 
 
 import java.sql.*;
 
 public class DBUtil {
   //其中mysql是數(shù)據(jù)庫(kù)名稱,在mysql57版本的數(shù)據(jù)庫(kù)中已經(jīng)預(yù)先新建完成;3306是mysql數(shù)據(jù)庫(kù)的端口號(hào)。
   private static String url="jdbc:mysql://localhost:3306/mysql";
   //com.mysql.jdbc.Driver是mysql-connector-java-5.1.40中的驅(qū)動(dòng)包路徑
   private static String driverClass="com.mysql.jdbc.Driver";
   //mysql的賬號(hào)和密碼是在安裝mysql中進(jìn)行設(shè)置的,這里拿來用即可。
   private static String username="root";
   private static String password="123456";
   private static Connection conn;
   //裝載驅(qū)動(dòng)
   static{
     try{
       Class.forName(driverClass);
     }
     catch(ClassNotFoundException e){
       e.printStackTrace();
     }
   }
   //獲取數(shù)據(jù)庫(kù)連接
   public static Connection getConnection(){
     try{
       conn=DriverManager.getConnection(url,username,password);
     }
     catch(SQLException e){
       e.printStackTrace();
     }
     return conn;
   }
   //建立數(shù)據(jù)庫(kù)連接
   public static void main(String[] args){
     Connection conn=DBUtil.getConnection();
     if(conn!=null){
       System.out.println("數(shù)據(jù)庫(kù)連接成功");
     }
     else{
       System.out.println("數(shù)據(jù)庫(kù)連接失敗");
     }
   }
   //關(guān)閉數(shù)據(jù)庫(kù)連接
   public static void Close(){
     if(conn!=null){
       try{
         conn.close();
       }
       catch(SQLException e){
         e.printStackTrace();
       }
     }
   }
 }

d、運(yùn)行服務(wù)器,測(cè)試是否成功搭建。

Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互

Android應(yīng)用中怎么與Apache Tomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互

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 
           List<NameValuePair> params=new ArrayList<NameValuePair>();//將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ù)器實(shí)現(xiàn)數(shù)據(jù)交互的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當(dāng)前文章:Android應(yīng)用中怎么與ApacheTomcat服務(wù)器實(shí)現(xiàn)數(shù)據(jù)交互
網(wǎng)頁(yè)地址:http://bm7419.com/article4/jjssie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站排名、定制開發(fā)、靜態(tài)網(wǎng)站、網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)