Android+java:servlet+navicat:MySQL:登陸注冊頁面 01(Android部分)
來源:我的學(xué)習(xí)筆記(此項(xiàng)目分兩頁這是第一頁)

項(xiàng)目結(jié)構(gòu)

先設(shè)置聯(lián)網(wǎng)權(quán)限:就一行,我就不提供代碼了


代碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
? ?tools:context=".MainActivity">
? ?<EditText
? ? ? ?android:id="@+id/id"
? ? ? ?android:layout_width="wrap_content"
? ? ? ?android:layout_height="wrap_content"
? ? ? ?android:layout_alignParentTop="true"
? ? ? ?android:layout_centerHorizontal="true"
? ? ? ?android:layout_marginTop="78dp"
? ? ? ?android:ems="10"
? ? ? ?android:inputType="textPersonName"
? ? ? ?android:hint="用戶名" />
? ?<EditText
? ? ? ?android:id="@+id/password"
? ? ? ?android:layout_width="wrap_content"
? ? ? ?android:layout_height="wrap_content"
? ? ? ?android:layout_alignEnd="@+id/id"
? ? ? ?android:layout_alignRight="@+id/id"
? ? ? ?android:layout_below="@+id/id"
? ? ? ?android:layout_marginTop="28dp"
? ? ? ?android:ems="10"
? ? ? ?android:inputType="textPassword"
? ? ? ?android:hint="密碼"/>
? ?<Button
? ? ? ?android:id="@+id/register"
? ? ? ?android:text="注冊"
? ? ? ?android:layout_marginTop="20dp"
? ? ? ?android:layout_width="wrap_content"
? ? ? ?android:layout_height="wrap_content"
? ? ? ?android:layout_below="@+id/password"
? ? ? ?android:layout_alignLeft="@+id/password"/>
? ?<Button
? ? ? ?android:id="@+id/login"
? ? ? ?android:text="登錄"
? ? ? ?android:layout_marginTop="20dp"
? ? ? ?android:layout_below="@id/password"
? ? ? ?android:layout_width="wrap_content"
? ? ? ?android:layout_height="wrap_content"
? ? ? ?android:layout_alignRight="@id/password"/>
</RelativeLayout>

代碼;
package com.example.demo0508;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
? ?private Button login,register;
? ?private EditText id,password;
? ?private final static int LOGIN_JUDGE = 1;
? ?private int RequestCode = 1;
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.activity_main);
? ? ? ?login = (Button) findViewById(R.id.login);
? ? ? ?login.setOnClickListener(this);
? ? ? ?register = (Button) findViewById(R.id.register);
? ? ? ?register.setOnClickListener(this);
? ? ? ?id = (EditText) findViewById(R.id.id);
? ? ? ?password = (EditText) findViewById(R.id.password);
? ?}
? ?@Override
? ?protected void onActivityResult(int requestCode, int resultCode, Intent data) {
? ? ? ?super.onActivityResult(requestCode, resultCode, data);
? ? ? ?if(requestCode==1&&resultCode==2){
? ? ? ? ? ?id.setText(data.getStringExtra("id"));
? ? ? ? ? ?password.setText(data.getStringExtra("password"));
? ? ? ?}
? ?}
? ?Handler handler = new Handler(){
? ? ? ?@Override
? ? ? ?public void handleMessage(Message msg){
? ? ? ? ? ?super.handleMessage(msg);
? ? ? ? ? ?switch (msg.what){
? ? ? ? ? ? ? ?case LOGIN_JUDGE:{
? ? ? ? ? ? ? ? ? ?Bundle bundle = new Bundle();
? ? ? ? ? ? ? ? ? ?bundle = msg.getData();
? ? ? ? ? ? ? ? ? ?String result = bundle.getString("result");
? ? ? ? ? ? ? ? ? ?//Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ? ? ?if (result.equals("success")) {
? ? ? ? ? ? ? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this,"登錄成功!",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this,"登錄失敗!",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?}catch (NullPointerException e){
? ? ? ? ? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?}
? ? ? ?}
? ?};
? ?@Override
? ?public void onClick(View view) {
? ? ? ?switch (view.getId()){
? ? ? ? ? ?case R.id.login:{
? ? ? ? ? ? ? ?new Thread(new Runnable() {
? ? ? ? ? ? ? ? ? ?@Override
? ? ? ? ? ? ? ? ? ?public void run() {
? ? ? ? ? ? ? ? ? ? ? ?//使用下面類里的函數(shù),連接servlet,返回一個(gè)result,使用handler處理這個(gè)result
? ? ? ? ? ? ? ? ? ? ? ?String result = HttpLogin.LoginByPost(id.getText().toString(),password.getText().toString());
? ? ? ? ? ? ? ? ? ? ? ?Bundle bundle = new Bundle();
? ? ? ? ? ? ? ? ? ? ? ?bundle.putString("result",result);
? ? ? ? ? ? ? ? ? ? ? ?Message message = new Message();
? ? ? ? ? ? ? ? ? ? ? ?message.setData(bundle);
? ? ? ? ? ? ? ? ? ? ? ?message.what = LOGIN_JUDGE;
? ? ? ? ? ? ? ? ? ? ? ?handler.sendMessage(message);
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}).start();
? ? ? ? ? ?}
? ? ? ? ? ?break;
? ? ? ? ? ?case R.id.register:{
? ? ? ? ? ? ? ?Intent intent = new Intent(this,RegisterActivity.class);
? ? ? ? ? ? ? ?startActivityForResult(intent,RequestCode);
? ? ? ? ? ?}
? ? ? ? ? ?break;
? ? ? ?}
? ?}
}

代碼;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
? ?tools:context=".RegisterActivity">
? ?<EditText
? ? ? ?android:id="@+id/id_edit"
? ? ? ?android:hint="用戶名"
? ? ? ?android:layout_marginTop="50dp"
? ? ? ?android:layout_width="match_parent"
? ? ? ?android:layout_height="wrap_content" />
? ?<EditText
? ? ? ?android:id="@+id/password_edit"
? ? ? ?android:layout_below="@+id/id_edit"
? ? ? ?android:hint="密碼"
? ? ? ?android:layout_marginTop="10dp"
? ? ? ?android:layout_width="match_parent"
? ? ? ?android:layout_height="wrap_content" />
? ?<EditText
? ? ? ?android:id="@+id/password_edit_1"
? ? ? ?android:hint="確認(rèn)密碼"
? ? ? ?android:layout_below="@+id/password_edit"
? ? ? ?android:layout_marginTop="10dp"
? ? ? ?android:layout_width="match_parent"
? ? ? ?android:layout_height="wrap_content" />
? ?<EditText
? ? ? ?android:id="@+id/email_edit"
? ? ? ?android:hint="郵箱"
? ? ? ?android:layout_below="@+id/password_edit_1"
? ? ? ?android:layout_marginTop="10dp"
? ? ? ?android:layout_width="match_parent"
? ? ? ?android:layout_height="wrap_content" />
? ?<LinearLayout
? ? ? ?android:layout_marginTop="20dp"
? ? ? ?android:layout_below="@+id/email_edit"
? ? ? ?android:layout_width="match_parent"
? ? ? ?android:layout_height="wrap_content">
? ? ? ?<Button
? ? ? ? ? ?android:text="返回"
? ? ? ? ? ?android:layout_weight="0.5"
? ? ? ? ? ?android:id="@+id/back_edit"
? ? ? ? ? ?android:layout_width="match_parent"
? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ?android:layout_below="@+id/email_edit"/>
? ? ? ?<Button
? ? ? ? ? ?android:id="@+id/register_do"
? ? ? ? ? ?android:text="注冊"
? ? ? ? ? ?android:layout_weight="0.5"
? ? ? ? ? ?android:layout_width="match_parent"
? ? ? ? ? ?android:layout_height="wrap_content" />
? ?</LinearLayout>
</RelativeLayout>

代碼;
package com.example.demo0508;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.*;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
? ?private int ResultCode = 2;
? ?private final static int REGISTER_JUDGE = 2;
? ?private Button register,back;
? ?private EditText id,psw_1,psw_2,email;
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.activity_register);
? ? ? ?register = (Button) findViewById(R.id.register_do);
? ? ? ?register.setOnClickListener(this);
? ? ? ?id = (EditText) findViewById(R.id.id_edit);
? ? ? ?psw_1 = (EditText) findViewById(R.id.password_edit);
? ? ? ?psw_2 = (EditText) findViewById(R.id.password_edit_1);
? ? ? ?email = (EditText) findViewById(R.id.email_edit);
? ?}
? ?Handler handler = new Handler(){
? ? ? ?@Override
? ? ? ?public void handleMessage(Message msg){
? ? ? ? ? ?super.handleMessage(msg);
? ? ? ? ? ?switch (msg.what){
? ? ? ? ? ? ? ?case REGISTER_JUDGE:{
? ? ? ? ? ? ? ? ? ?Bundle bundle = new Bundle();
? ? ? ? ? ? ? ? ? ?bundle = msg.getData();
? ? ? ? ? ? ? ? ? ?String result = bundle.getString("result");
? ? ? ? ? ? ? ? ? ?//Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ? ? ?if (result.equals("success")) {
? ? ? ? ? ? ? ? ? ? ? ? ? ?Intent intent = new Intent();
? ? ? ? ? ? ? ? ? ? ? ? ? ?intent.putExtra("id",id.getText().toString());
? ? ? ? ? ? ? ? ? ? ? ? ? ?intent.putExtra("password",psw_1.getText().toString());
? ? ? ? ? ? ? ? ? ? ? ? ? ?setResult(ResultCode,intent);//向上一級發(fā)送數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? ? ?finish();
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?}catch (NullPointerException e){
? ? ? ? ? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?}
? ? ? ?}
? ?};
? ?@Override
? ?public void onClick(View v) {
? ? ? ?switch (v.getId()){
? ? ? ? ? ?case R.id.register_do:{
? ? ? ? ? ? ? ?if( ! psw_1.getText().toString().equals(psw_2.getText().toString())){
? ? ? ? ? ? ? ? ? ?Toast.makeText(RegisterActivity.this,"兩次密碼不一致!",Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ?}else {
? ? ? ? ? ? ? ? ? ?new Thread(new Runnable() {
? ? ? ? ? ? ? ? ? ? ? ?@Override
? ? ? ? ? ? ? ? ? ? ? ?public void run() {
? ? ? ? ? ? ? ? ? ? ? ? ? ?String result = HttpLogin.RegisterByPost(id.getText().toString(),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?psw_1.getText().toString(),email.getText().toString());
? ? ? ? ? ? ? ? ? ? ? ? ? ?Bundle bundle = new Bundle();
? ? ? ? ? ? ? ? ? ? ? ? ? ?bundle.putString("result",result);
? ? ? ? ? ? ? ? ? ? ? ? ? ?Message msg = new Message();
? ? ? ? ? ? ? ? ? ? ? ? ? ?msg.what = REGISTER_JUDGE;
? ? ? ? ? ? ? ? ? ? ? ? ? ?msg.setData(bundle);
? ? ? ? ? ? ? ? ? ? ? ? ? ?handler.sendMessage(msg);
? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?}).start();
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?break;
? ? ? ?}
? ?}
}

代碼:
package com.example.demo0508;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
public class HttpLogin {
? ?public static String LoginByPost(String id,String password){
? ? ? ?String address = "http://192.168.242.76:8080/demo0508/AndroidLogin";//設(shè)置自己的IP
? ? ? ?String result = "";
? ? ? ?try{
? ? ? ? ? ?URL url = new URL(address);//初始化URL
? ? ? ? ? ?HttpURLConnection conn = (HttpURLConnection) url.openConnection();
? ? ? ? ? ?conn.setRequestMethod("POST");//請求方式
? ? ? ? ? ?//超時(shí)信息
? ? ? ? ? ?conn.setReadTimeout(5000);
? ? ? ? ? ?conn.setConnectTimeout(5000);
? ? ? ? ? ?//post方式不能設(shè)置緩存,需手動(dòng)設(shè)置為false
? ? ? ? ? ?conn.setUseCaches(false);
? ? ? ? ? ?//我們請求的數(shù)據(jù)
? ? ? ? ? ?String data = "id="+ URLEncoder.encode(id,"UTF-8")+
? ? ? ? ? ? ? ? ? ?"&password="+ URLEncoder.encode(password,"UTF-8");
? ? ? ? ? ?//獲取輸出流
? ? ? ? ? ?OutputStream out = conn.getOutputStream();
? ? ? ? ? ?out.write(data.getBytes());
? ? ? ? ? ?out.flush();
? ? ? ? ? ?out.close();
? ? ? ? ? ?conn.connect();
? ? ? ? ? ?if (conn.getResponseCode() == 200) {
? ? ? ? ? ? ? ?// 獲取響應(yīng)的輸入流對象
? ? ? ? ? ? ? ?InputStream is = conn.getInputStream();
? ? ? ? ? ? ? ?// 創(chuàng)建字節(jié)輸出流對象
? ? ? ? ? ? ? ?ByteArrayOutputStream message = new ByteArrayOutputStream();
? ? ? ? ? ? ? ?// 定義讀取的長度
? ? ? ? ? ? ? ?int len = 0;
? ? ? ? ? ? ? ?// 定義緩沖區(qū)
? ? ? ? ? ? ? ?byte buffer[] = new byte[1024];
? ? ? ? ? ? ? ?// 按照緩沖區(qū)的大小,循環(huán)讀取
? ? ? ? ? ? ? ?while ((len = is.read(buffer)) != -1) {
? ? ? ? ? ? ? ? ? ?// 根據(jù)讀取的長度寫入到os對象中
? ? ? ? ? ? ? ? ? ?message.write(buffer, 0, len);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?// 釋放資源
? ? ? ? ? ? ? ?is.close();
? ? ? ? ? ? ? ?message.close();
? ? ? ? ? ? ? ?// 返回字符串
? ? ? ? ? ? ? ?result = new String(message.toByteArray());
? ? ? ? ? ? ? ?//return result;
? ? ? ? ? ?}
? ? ? ?}catch (MalformedURLException e){
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}catch (IOException e){
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ? ? ?return result;
? ?}
? ?public static String RegisterByPost(String id,String password,String email){
? ? ? ?String address = "http://192.168.242.76:8080/demo0508/androidregister";
? ? ? ?String result = "";
? ? ? ?try{
? ? ? ? ? ?URL url = new URL(address);//初始化URL
? ? ? ? ? ?HttpURLConnection conn = (HttpURLConnection) url.openConnection();
? ? ? ? ? ?conn.setRequestMethod("POST");//請求方式
? ? ? ? ? ?//超時(shí)信息
? ? ? ? ? ?conn.setReadTimeout(5000);
? ? ? ? ? ?conn.setConnectTimeout(5000);
? ? ? ? ? ?//post方式不能設(shè)置緩存,需手動(dòng)設(shè)置為false
? ? ? ? ? ?conn.setUseCaches(false);
? ? ? ? ? ?//我們請求的數(shù)據(jù)
? ? ? ? ? ?String data = "id="+ URLEncoder.encode(id,"UTF-8")+
? ? ? ? ? ? ? ? ? ?"&password="+ URLEncoder.encode(password,"UTF-8")+
? ? ? ? ? ? ? ? ? ?"&email="+ URLEncoder.encode(email,"UTF-8");
? ? ? ? ? ?//獲取輸出流
? ? ? ? ? ?OutputStream out = conn.getOutputStream();
? ? ? ? ? ?out.write(data.getBytes());
? ? ? ? ? ?out.flush();
? ? ? ? ? ?out.close();
? ? ? ? ? ?conn.connect();
? ? ? ? ? ?if (conn.getResponseCode() == 200) {
? ? ? ? ? ? ? ?// 獲取響應(yīng)的輸入流對象
? ? ? ? ? ? ? ?InputStream is = conn.getInputStream();
? ? ? ? ? ? ? ?// 創(chuàng)建字節(jié)輸出流對象
? ? ? ? ? ? ? ?ByteArrayOutputStream message = new ByteArrayOutputStream();
? ? ? ? ? ? ? ?// 定義讀取的長度
? ? ? ? ? ? ? ?int len = 0;
? ? ? ? ? ? ? ?// 定義緩沖區(qū)
? ? ? ? ? ? ? ?byte buffer[] = new byte[1024];
? ? ? ? ? ? ? ?// 按照緩沖區(qū)的大小,循環(huán)讀取
? ? ? ? ? ? ? ?while ((len = is.read(buffer)) != -1) {
? ? ? ? ? ? ? ? ? ?// 根據(jù)讀取的長度寫入到os對象中
? ? ? ? ? ? ? ? ? ?message.write(buffer, 0, len);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?// 釋放資源
? ? ? ? ? ? ? ?is.close();
? ? ? ? ? ? ? ?message.close();
? ? ? ? ? ? ? ?// 返回字符串
? ? ? ? ? ? ? ?result = new String(message.toByteArray());
? ? ? ? ? ? ? ?//return result;
? ? ? ? ? ?}
? ? ? ?}catch (MalformedURLException e){
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}catch (IOException e){
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ? ? ?return result;
? ?}
}
知識(shí)聯(lián)系:《《《《《《《《《《《本機(jī)IP地址這樣查》》》》》》》》》》》》
1.win+r

2.查詢命令+回車

3.
