团队冲刺(四)

一、认领任务

经过团队讨论,我认领到的工作是:实现登录页面的实现

二、任务完成时间估算

页面布局 2h
从数据库提取 5h

登录页面

展示如下:

 

主要的实现代码:

LoginActivity

package com.example.myapplication5;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class LoginActivity extends AppCompatActivity {
private EditText editText;
private EditText editText2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();

setContentView(R.layout.activity_main2);
//获取用户名的id
editText = (EditText) findViewById(R.id.editText);
//获取密码的id
editText2 = (EditText) findViewById(R.id.editText2);

Button button1 = (Button)LoginActivity.this.findViewById(R.id.button10);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}

public void LoginPost(View view){
//获取用户名的值
String name=editText.getText().toString();
//获取密码的值
String password=editText2.getText().toString();
//判断用户名或密码是否为空
if(TextUtils.isEmpty(name)||TextUtils.isEmpty((password))){
Toast.makeText(getApplicationContext(),"用户名或密码为空",Toast.LENGTH_LONG).show();//Toast

}else{
//获取网络上的servlet路径
String path="http://10.0.2.2:8080/testhttp/Login.Servlet";
//调用postTask,把获取到的用户名,密码与路径放入方法中
new postTask().execute(name,password,path);}//Params:开始异步任务执行时传入的参数类型,对应excute()中传递的参数
}

class postTask extends AsyncTask{

@Override
protected Object doInBackground(Object[] params) {
//依次获取用户名,密码与路径
String name=params[0].toString();
String password=params[1].toString();
String path=params[2].toString();
try {
//获取网络上get方式提交的整个路径
URL url=new URL(path);
//打开网络连接
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
//设置提交方式
conn.setRequestMethod("POST");
//设置网络超时时间
conn.setConnectTimeout(5000);
//界面上所有的参数名加上他的值
String s="name="+name+"&password="+password;
//获取请求头
conn.setRequestProperty("Content-Length",s.length()+"");//键是固定的
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");//键和值是固定的
//设置允许对外输出数据
conn.setDoOutput(true);
//把界面上的所有数据写出去
OutputStream os=conn.getOutputStream();
os.write(s.getBytes());
if(conn.getResponseCode()==200){
//用io流与web后台进行数据交互
InputStream is=conn.getInputStream();
//字节流转字符流
BufferedReader br=new BufferedReader(new InputStreamReader(is));
//读出每一行的数据
String str=br.readLine();
//返回读出的每一行的数据
return str;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
//获取Android studio与web后台数据交互获得的值
String s= (String) o;

String[] rs = s.split("[||]");
// 吐司Android studio与web后台数据交互获得的值
System.out.println("s1");
System.out.println(s);
System.out.println("0 "+rs[0]);
System.out.println("1 "+rs[1]);
System.out.println("2 "+rs[2]);
System.out.println("3 "+rs[3]);
System.out.println("4 "+rs[4]);
// Toast.makeText(LoginActivity.this, rs[0], Toast.LENGTH_SHORT).show();
String name=editText.getText().toString();
if(rs[2]!=null){
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("name", name);
intent.putExtra("user",rs[4]);
intent.putExtra("Szong",rs[6]);
startActivity(intent);
Toast.makeText(LoginActivity.this, rs[1], Toast.LENGTH_SHORT).show();
}else{
Intent intent = new Intent(LoginActivity.this,LoginActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, rs[0], Toast.LENGTH_SHORT).show();
}
}
}
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"

android:background="@drawable/runbg4">

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.3" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />

<TextView
android:id="@+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prompt_email"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.769" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/ehint"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.48"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.76" />

<TextView
android:id="@+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prompt_password"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/phint"
android:inputType="textPassword"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
android:id="@+id/button9"
android:layout_width="124dp"
android:layout_height="59dp"
android:onClick="LoginPost"
android:text="@string/login"
android:background="@drawable/button_drawable"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline3"
app:layout_constraintVertical_bias="0.179" />

<Button
android:id="@+id/button10"
android:layout_width="124dp"
android:layout_height="59dp"
android:text="注册"
android:background="@drawable/button_drawable"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline3"
app:layout_constraintVertical_bias="0.555" />

</androidx.constraintlayout.widget.ConstraintLayout>

下面是后台的实现:

bean层:


public class User {
private String name;
private String password;
private String vname;
private String weight;
private String height;
private String year;
private String school;
private String sex;
public String getVname() {
return vname;
}
public void setVname(String vname) {
this.vname = vname;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

dao层:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import com.test.util.*;
public class LoginDao {
public static String searchUsername(String name) {
String sql="select * from user where ";
if(name!=null) {
sql=sql+"name like+'%"+name+"%'";
}
Connection conn=DBUtil.getConn();
Statement state=null;
ResultSet rs=null;
String name1=null;
try {
state=conn.createStatement();
rs=state.executeQuery(sql);
while(rs.next())
{
name1=rs.getString("name");

}
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(rs,state, conn);
}
return name1;

}
public static String searchPassword(String name) {
String sql="select * from user where ";
if(name!=null) {
sql=sql+"name like '%"+name+"%'";
}
Connection conn=DBUtil.getConn();
Statement state=null;
ResultSet rs=null;
String password1=null;
try {
state=conn.createStatement();
rs=state.executeQuery(sql);
while(rs.next()) {
password1=rs.getString("password");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtil.close(rs,state,conn);
}
return password1;
}
public boolean GetMessage(String name) {
String sql="select * from message where name ="+ name;
//创建数据库链接
Connection conn = DBUtil.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
DBUtil.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}

}

servlet层:

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 com.alibaba.fastjson.JSON;
import com.test.bean.Date;
import com.test.bean.User;
import com.test.dao.*;
import java.util.List;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
LoginDao logindao = new LoginDao();
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
// protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.setCharacterEncoding("UTF-8");
// String name=request.getParameter("name");
// String password=request.getParameter("password");
// String rname=logindao.searchUsername(name);
// String rpassword=logindao.searchPassword(name);
// if(name.equals(rname)&&password.equals(rpassword)) {
// request.getRequestDispatcher("index.jsp").forward(request, response);
// System.out.println("IndexServlet跳转成功");
// }
// else {
// request.getRequestDispatcher("denglu.jsp").forward(request,response);
// System.out.println("IndexServlet跳转失败");
// System.out.println(name+password);
// System.out.println(rname+rpassword);
// }
// }

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String password=request.getParameter("password");
String a=null;
System.out.println(name);
System.out.println(password);
String rname=logindao.searchUsername(name);
String rpassword=logindao.searchPassword(name);
PrintWriter pw=response.getWriter();
if(name.equals(rname)&&password.equals(rpassword)) {
UserDao ud = new UserDao();
List<User> list = ud.select(name);
String json = JSON.toJSONString(list);
DateShowDao dsd = new DateShowDao();
List<Date> list1 = dsd.select1(name);
int t=0;
for(Date date:list1) {
String[] my =date.getTime().split(":");
int hour =Integer.parseInt(my[0]);
int min =Integer.parseInt(my[1]);
int sec =Integer.parseInt(my[2]);
int zong =hour*3600+min*60+sec;
t=t+zong;
// System.out.println(date.getName()+zong+" "+t);
}
String str = String.valueOf(t);//运动时长

System.out.println(json);
a="登录成功";
pw.write(a+"||"+name+"||"+json+"||"+str+"||");//+str+"||"
pw.close();
}
else {
a="用户名或密码错误";
pw.write(a+"||");
pw.close();
}




}
}

这样子登录功能就能实现了。

原文地址:https://www.cnblogs.com/a155-/p/12790698.html