日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 本地tomcat服务器接收处理手机上传的数据之案例演示

發布時間:2023/12/31 Android 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 本地tomcat服务器接收处理手机上传的数据之案例演示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上一篇:Android 本地tomcat服務器接收處理手機上傳的數據之環境搭建 ? ? 本篇基于上一篇搭建的服務器端環境,具體介紹Android真機上傳數據到tomcat服務器的交互過程 ? 場景:Android客戶端上傳用戶名和密碼到tomcat服務端,tomcat服務器自動接收Android客戶端上傳的數據,并打印出結果 ? 一、tomcat服務器端實現 ? 1.首先啟動tomcat服務器 ?

?

?

?

? 直接點擊“finish”即可,啟動后效果如下: ?

?

2. 編寫servlet實現:ServletDemo1.java ? package com.servlet.demo; ? import java.io.IOException; import java.io.PrintWriter; ? 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 org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; ? /** * Servlet implementation class ServletDemo1 */ @WebServlet(asyncSupported = true, urlPatterns = { "/ServletDemo1" }) public class ServletDemo1 extends HttpServlet { ????private static final long serialVersionUID = 1L; ? ????private static Log Log = LogFactory.getLog(ServletDemo1.class); ? ????/** ?????* The doGet method of the servlet. <br> ?????* ?????* This method is called when a form has its tag value method equals to get. ?????* ?????* @param request ?????*????????????the request send by the client to the server ?????* @param response ?????*????????????the response send by the server to the client ?????* @throws ServletException ?????*?????????????if an error occurred ?????* @throws IOException ?????*?????????????if an error occurred ?????*/ ????public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ????????response.setContentType("text/html"); ????????PrintWriter out = response.getWriter(); ????????out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); ????????out.println("<HTML>"); ????????out.println("??<HEAD><TITLE>A Servlet</TITLE></HEAD>"); ????????out.println("??<BODY>"); ????????out.print("????This is "); ????????out.print(this.getClass()); ????????out.println(", using the GET method"); ????????out.println("??</BODY>"); ????????out.println("</HTML>"); ????????out.flush(); ????????out.close(); ? ????????// 獲取請求的數據,并向控制臺輸出 ????????String username = request.getParameter("username"); ????????String password = request.getParameter("password"); ????????System.out.println("-----> doGet username:" + username + "???password:" + password); ????} ? ????/** ?????* The doPost method of the servlet. <br> ?????* ?????* This method is called when a form has its tag value method equals to ?????* post. ?????* ?????* @param request ?????*????????????the request send by the client to the server ?????* @param response ?????*????????????the response send by the server to the client ?????* @throws ServletException ?????*?????????????if an error occurred ?????* @throws IOException ?????*?????????????if an error occurred ?????*/ ????public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ????????response.setContentType("text/html"); ????????PrintWriter out = response.getWriter(); ????????out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); ????????out.println("<HTML>"); ????????out.println("??<HEAD><TITLE>A Servlet</TITLE></HEAD>"); ????????out.println("??<BODY>"); ????????out.print("????This is "); ????????out.print(this.getClass()); ????????out.println(", using the POST method"); ????????out.println("??</BODY>"); ????????out.println("</HTML>"); ????????out.flush(); ????????out.close(); ? ????????// 獲取請求的數據,并向控制臺輸出 ????????String username = request.getParameter("username"); ????????String password = request.getParameter("password"); ????????System.out.println("-----> doPost username:" + username + "???password:" + password); ????} } ? 3.運行Servlet

?

運行成功:

?

? 二、Android客戶端實現 ? 1. 主頁?MainActivity ? package com.example.client; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; ? public class MainActivity extends Activity { ??? private EditText username; ??? private EditText password; ??? private Button signup; ??? @Override ??? protected void onCreate(Bundle savedInstanceState) { ??????? super.onCreate(savedInstanceState); ??????? setContentView(R.layout.activity_main); ??????? username = (EditText) findViewById(R.id.account); ??????? password = (EditText) findViewById(R.id.password); ??????? signup = (Button) findViewById(R.id.btnSign); ??????? signup.setOnClickListener(new View.OnClickListener() { ??????????? @Override ??????????? public void onClick(View v) { ??????????????? onLogin(); ??????????? } ??????? }); ??? } ??? // 發起HTTP請求 ??? public void onLogin() { ??????? String url = "http://192.168.191.1:8080/First/test/ServletDemo1"; ??????? new HttpTread(url, username.getText().toString(), password.getText().toString()).start(); ??? } } ? 說明: 1. 上述 "http://192.168.191.1:8080/First/test/ServletDemo1",其中ip地址的確定方法,請查看《Android 本地搭建Tomcat服務器供真機測試》介紹 2.? First:是web工程名 3.??test/ServletDemo1:是如下高亮部分映射的名稱 ?

?

? 2. 線程HttpTread ? package com.example.client; ? import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; ? public class HttpTread extends Thread { ????String url; ????String username; ????String password; ? ????public HttpTread(String url, String username, String password) { ????????this.url = url; ????????this.username = username; ????????this.password = password; ????} ? ????private void send() throws IOException { ????????// 將username和password傳給Tomcat服務器 ????????url = url + "?username=" + username + "&password=" + password; ????????try { ? ????????????URL httpUrl = new URL(url); ????????????// 獲取網絡連接 ????????????HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); ????????????// 設置請求方法為GET方法 ????????????conn.setRequestMethod("GET"); // 或 "POST" ????????????// 設置訪問超時時間 ????????????conn.setReadTimeout(5000); ????????????BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); ????????????String str; ????????????StringBuffer sb = new StringBuffer(); ????????????// 讀取服務器返回的信息 ????????????while ((str = reader.readLine()) != null) { ????????????????sb.append(str); ????????????} ????????????// 把服務端返回的數據打印出來 ????????????System.out.println("HttpTreadResult:" + sb.toString()); ????????} catch (MalformedURLException e) { ????????} ????} ? ????@Override ????public void run() { ????????super.run(); ????????try { ????????????send(); ????????} catch (IOException e) { ????????????e.printStackTrace(); ????????} ????} } ? 3.?AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ??? package="com.example.client" ??? android:versionCode="1" ??? android:versionName="1.0" > ??? <uses-sdk ??????? android:minSdkVersion="25" ??????? android:targetSdkVersion="25" /> ??? <uses-permission android:name="android.permission.INTERNET" /> ??? <application ??????? android:allowBackup="true" ??????? android:icon="@drawable/ic_launcher" ??????? android:label="@string/app_name" ??????? android:theme="@style/AppTheme" > ??????? <activity ??????????? android:name=".MainActivity" ??????????? android:label="@string/app_name" > ??????????? <intent-filter> ??????????????? <action android:name="android.intent.action.MAIN" /> ??????????????? <category android:name="android.intent.category.LAUNCHER" /> ??????????? </intent-filter> ??????? </activity> ??? </application> </manifest> ? 說明: 需要聲明權限:<uses-permission android:name="android.permission.INTERNET" /> ? 編碼結束。 ? 4. Android 客戶端主頁面:

?

填寫用戶名和密碼,例如:用戶名:aaaa 密碼:bbb123 ?

?

? 點擊“提交”按鈕。切換到Eclipse中,可以看到Tomcat自動打印出所提交的數據: ?

?

完成。 ? Android客戶端和tomcat服務端完整源碼

轉載于:https://www.cnblogs.com/onelikeone/p/9963142.html

總結

以上是生活随笔為你收集整理的Android 本地tomcat服务器接收处理手机上传的数据之案例演示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。