Android 與javaEE後臺網絡連接測試

本人是Android前端程序員,這段時間一直在學習Java後臺,正好學到Servlet這一節,忽然想到之前Android請求後臺數據,正好試試,此次主要測試Android前端與請求後臺,過程以下:
一.用eclipse寫一個servlet接受數據,接受到請求就打印數據html

1>簡單servlet的java代碼
public class LoginServlet extends HttpServlet {
@Override
public void init() throws ServletException {
    super.init();
    int i=0;
    getServletContext().setAttribute("count", i);
}
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.接受參數
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username=="+username);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
2>在web.xml註冊servlet
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WEB13</display-name>
  
  <servlet>
      <servlet-name>LoginServlet</servlet-name>
      <servlet-class>com.itheima.login.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>LoginServlet</servlet-name>
      <url-pattern>/login</url-pattern>
  </servlet-mapping>
  
</web-app>

3.測試一下寫的有沒有問題前端

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WEB14/login" method="post"><br/>
用戶名:<input type="text" name="username"><br/>
用戶名:<input type="password" name="password"><br/>
<input type="submit" value="登錄">

</form>

</body>
</html>

運行效果以下:java

clipboard.png
點擊登錄:android

clipboard.png
後臺測試成功
二.寫一個簡單的Android登錄頁面程序員

clipboard.png
佈局代碼:web

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent"

    tools:context="com.example.creator.hhren.ui.LoginActivity">

    <ScrollView

        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="80dp"
                android:text="Welcome"
                android:textColor="#454553"
                android:textSize="57sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginTop="85dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <RelativeLayout
                    android:layout_width="80dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:text="用戶名 : "
                        android:textSize="16sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="40dp"
                    android:background="@mipmap/lon_edt_bg"
                    android:gravity="center_vertical">

                    <EditText
                        android:id="@+id/edt_username"
                        android:layout_width="match_parent"
                        android:layout_height="35dp"
                        android:layout_marginLeft="10dp"
                        android:background="@color/transp"
                        android:hint="請輸入用戶名"
                        android:inputType="number"
                        android:textCursorDrawable="@null"
                        android:textSize="13sp" />
                </RelativeLayout>


            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginTop="20dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <RelativeLayout
                    android:layout_width="80dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:text="密 碼   :  "
                        android:textSize="16sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="40dp"
                    android:background="@mipmap/lon_edt_bg"
                    android:gravity="center_vertical">

                    <EditText
                        android:id="@+id/edt_pwd"
                        android:layout_width="match_parent"
                        android:layout_height="35dp"
                        android:layout_marginLeft="10dp"
                        android:background="@color/transp"
                        android:hint="請輸入用戶名"
                        android:inputType="number"
                        android:textCursorDrawable="@null"
                        android:textSize="13sp" />
                </RelativeLayout>
            </LinearLayout>

            <Button
                android:id="@+id/btn_login"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:layout_marginTop="80dp"
                android:background="@drawable/btn_selector"
                android:text="登陸"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:padding="20dp"
                />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

實現點擊登錄連接後臺:tomcat

clipboard.png

clipboard.png

clipboard.png

測試條件:
後臺tomcat運行的電腦和手機要在同一個局域網內(鏈接同一個WIFI)
使用cmd->ipcofig查看電腦的ip,
在Android中請求URL
clipboard.png
點擊登錄,請求效果app

clipboard.png

本文純屬新手娛樂,大神繞道eclipse

相關文章
相關標籤/搜索