国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android studio 通過mysql連接數(shù)據(jù)庫完成注冊登錄,登陸后通過tcp協(xié)議與電腦的網(wǎng)絡調試助手互發(fā)信息

這篇具有很好參考價值的文章主要介紹了Android studio 通過mysql連接數(shù)據(jù)庫完成注冊登錄,登陸后通過tcp協(xié)議與電腦的網(wǎng)絡調試助手互發(fā)信息。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

先不多直接看軟件截圖

這個是首頁等陸界面

android項目mysql登錄,android-studio

xml代碼如下

<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:layout_height="match_parent"
    android:background="@drawable/port"
    tools:context=".MainActivity"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_marginTop="20dp"
        android:gravity="center">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/jxlkjlog" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/welcome"
            android:textColor="#000000"
            android:textSize="20.5sp"
            android:textStyle="bold" />



    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginStart="50dp"
        android:layout_marginTop="200dp"
        android:layout_marginEnd="50dp"
        >

        <TextView
            android:id="@+id/text_username"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:text="@string/username"
            android:textColor="#000000"
            android:textSize="25sp"
            android:textStyle="bold">

        </TextView>

        <EditText
            android:id="@+id/Edit_username"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_toEndOf="@id/text_username"
            android:hint="用戶名不少于三位"
            android:inputType="text"
            android:autofillHints=""></EditText>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginStart="50dp"

        android:layout_marginEnd="50dp">

        <TextView
            android:id="@+id/text_passwold"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:text="@string/passwold"
            android:textColor="#000000"
            android:textSize="25sp"
            android:textStyle="bold">

        </TextView>

        <EditText
            android:id="@+id/Edit_passwold"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_toEndOf="@id/text_passwold"
            android:inputType="textPassword"
            android:hint="密碼不少于四位"

            ></EditText>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <Button
            android:id="@+id/btn_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/login"
            tools:ignore="ButtonStyle">



        </Button>

        <Button
            android:id="@+id/btn_enroll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/enroll"
            tools:ignore="ButtonStyle">

        </Button>
    </LinearLayout>

</LinearLayout>

MainActivity文件

public class MainActivity extends AppCompatActivity {
private Button mButtonLongin;
private Button mButtonEnroll;
private EditText mEditTextUsername;
private EditText mEditTextPassword;
private String username;
private String password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButtonLongin=findViewById(R.id.btn_login);
        mButtonEnroll=findViewById(R.id.btn_enroll);
        mEditTextUsername=findViewById(R.id.Edit_username);
        mEditTextPassword=findViewById(R.id.Edit_passwold);
        UserDao userDao=new UserDao();
        mButtonEnroll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,MainActivity3.class);
                startActivity(intent);
            }

        });
        mButtonLongin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        EditText editTextusername=findViewById(R.id.Edit_username);
        EditText editTextpassword=findViewById(R.id.Edit_passwold);
                if (editTextusername.length() < 3) {
//                    Toast.makeText(MainActivity.this, "用戶名不能小于三位", Toast.LENGTH_LONG).show();
                    CommonUtils.showLontMsg(MainActivity.this, "用戶名不能小于三位");

                } else if (editTextpassword.length() < 4) {

                    CommonUtils.showLontMsg(MainActivity.this, "密碼不能小于四位");

                }else
                {
                    new Thread() {
                        public void run() {
                          boolean aa=  userDao.longin(editTextusername.getText().toString(),editTextpassword.getText().toString());
                          if(aa==true){

                              Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                              startActivity(intent);

                          }else{
                              Looper.prepare();
                             CommonUtils.showDlgMsg(MainActivity.this, "賬號密碼不正確");
                             Looper.loop();
                          }

                        }
                    }.start();
                }
            }



        });

    }

以下是注冊界面

android項目mysql登錄,android-studio?

?注冊界面的xlm文件? 這個界面比較簡單就不介紹了

?

<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:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity3">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center"
        android:gravity="center"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/jxlkjlog" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/welcome"
            android:textColor="#000000"
            android:textSize="20.5sp"
            android:textStyle="bold" />



    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginStart="50dp"
        android:layout_marginTop="200dp"
        android:layout_marginEnd="50dp"
        android:layout_margin="5dp"
        >

        <TextView
            android:id="@+id/text_username"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="@string/username"
            android:textColor="#000000"
            android:textSize="25sp"
            android:textStyle="bold"
            >

        </TextView>

        <EditText
            android:id="@+id/Edit_username"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_toEndOf="@id/text_username"
            android:hint="用戶名不少于三位"
            android:inputType="text"

            android:autofillHints=""></EditText>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginStart="50dp"
        android:layout_marginEnd="50dp"
        android:layout_margin="5dp"
        >

        <TextView
            android:id="@+id/text_passwold"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="@string/passwold"
            android:textColor="#000000"
            android:textSize="25sp"
            android:textStyle="bold">

        </TextView>

        <EditText
            android:id="@+id/Edit_passwold"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_toEndOf="@id/text_passwold"
            android:inputType="textPassword"
            android:hint="密碼不少于四位"
            ></EditText>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginStart="50dp"
        android:layout_marginEnd="50dp"
        android:layout_margin="5dp">
        <TextView
            android:id="@+id/text_name"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="@string/name"
            android:textColor="#000000"
            android:textSize="25sp"
            android:textStyle="bold"

            >


        </TextView>

        <EditText
            android:id="@+id/Edit_name"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_toEndOf="@id/text_name"
            android:inputType="text"
            android:hint="請輸入姓名"

            ></EditText>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginStart="50dp"
        android:layout_margin="5dp"
        android:layout_marginEnd="50dp">
        <TextView
            android:id="@+id/text_idcard"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="@string/idcard"
            android:textColor="#000000"
            android:textSize="25sp"
            android:textStyle="bold"
            >


        </TextView>

        <EditText
            android:id="@+id/Edit_idcard"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_toEndOf="@id/text_idcard"
            android:inputType="text"
            android:hint="輸入身份證號碼"

            ></EditText>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginStart="50dp"
        android:layout_margin="5dp"
        android:layout_marginEnd="50dp">
        <TextView
            android:id="@+id/text_phonenumber"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="@string/phonenumber"
            android:textColor="#000000"
            android:textSize="25sp"
            android:textStyle="bold"

            >


        </TextView>

        <EditText
            android:id="@+id/Edit_phonenumber"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_toEndOf="@id/text_phonenumber"
            android:inputType="text"
            android:hint="請輸入手機號"

            ></EditText>
    </RelativeLayout>
<LinearLayout
    android:layout_width="280dp"
    android:layout_height="60dp"
    android:layout_gravity="center"
    android:layout_marginLeft="18dp">
    <Button
        android:id="@+id/register"
        android:layout_width="130dp"
        android:layout_height="60dp"
        android:text="注冊"
        tools:ignore="ButtonStyle">

    </Button>

    <Button
        android:id="@+id/cancel"
        android:layout_width="130dp"
        android:layout_height="60dp"
        android:text="返回"
        tools:ignore="ButtonStyle">

    </Button>

</LinearLayout>

</LinearLayout>

MainActivity文件

package com.example.longin_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.longin_demo.tcp.ConnectThread;
import com.example.longin_demo.tcp.Send;

public class MainActivity2 extends AppCompatActivity {
    private Button mBtn_send;
    private Button mBtn_connect;
    private EditText mEt_send;
    private static TextView mTv_recv;
    private String ip;
    private int port;
    private String msg;
    private ConnectThread ct;
    private Send send;
    private Handler handler;
    static Receive re = new Receive();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mTv_recv = findViewById(R.id.receive);
        mBtn_connect = findViewById(R.id.connect);
        mBtn_send = findViewById(R.id.btn_1);


        final EditText mEt_ip = this.findViewById(R.id.ip);
        final EditText mEt_port = this.findViewById(R.id.port);
        final EditText mEt_send = this.findViewById(R.id.send);

/*
設置一個點擊事件用以連接線程
*/
        mBtn_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ip = mEt_ip.getText().toString();//獲取輸入的ip
                port = Integer.parseInt(mEt_port.getText().toString());//獲取輸入的端口號

                ct = new ConnectThread(ip, port,re);//創(chuàng)建一個線程來處理消息的收發(fā)
                ct.start();
            }
        });
        /*
設計一個點擊事件用以發(fā)送消息
*/
        mBtn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                msg = mEt_send.getText().toString();
                send = new Send(msg, port, ip);
                send.start();

            }
        });

    }
    /*
    這里是和連接接收線程中的 re.setString(string) 聯(lián)動的,自己理解一下怎么走在腦子里面走一次就差不多了。
    */
    public static class Receive{
        private  String string;
        public String getString(){
            return string;
        }
        public void setString(String s){
            this.string = s;
        }
    }
    /*
    下面的callback和連接接收線程中的 MainActivity.callback() 關聯(lián),線程運行到那一步后結束回到MainActivity中。
    */
    public static void callback(){
        System.out.println("連接線程執(zhí)行結束");
        mTv_recv.setText(re.getString());//這里是將接收到的文字顯示在接收框內。
    }

    }

這是一個注冊失敗的界面,如果數(shù)據(jù)庫內有相同的賬號密碼則顯示注冊失敗

android項目mysql登錄,android-studio

話不多說直接上代碼

<?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:layout_height="match_parent"
    tools:context=".FailRegister"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/no_register"
            android:gravity="center"
            android:textColor="#fff"
            android:background="@color/design_default_color_primary_dark"
            android:textSize="30sp"
            android:textStyle="bold"
            />

    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginStart="50dp"
        android:layout_marginTop="200dp"
        android:layout_marginEnd="50dp"
        >

        <ImageView

            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:src="@drawable/cw">

        </ImageView>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginStart="50dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="50dp"
        android:gravity="center"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no_register"
            android:gravity="center"
            android:textSize="30dp"
            android:textStyle="bold"
            />
    </RelativeLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginTop="210dp"
        android:layout_marginEnd="10dp"
        android:gravity="center">
        <Button
            android:id="@+id/home_back"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:text="@string/backtrack"
            tools:ignore="ButtonStyle"
            />
        <Button
            android:id="@+id/go_continue"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:text="@string/continue_register"
            tools:ignore="ButtonStyle"/>
    </LinearLayout>
</LinearLayout>

MainActivity里面的代碼

package com.example.longin_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FailRegister extends AppCompatActivity {

    private Button home_back;
    private Button go_continue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fail_register);
        home_back=findViewById(R.id.home_back);
        go_continue=findViewById(R.id.go_continue);
        home_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FailRegister.this, MainActivity.class);
                startActivity(intent);
            }
        });
        go_continue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FailRegister.this, MainActivity3.class);
                startActivity(intent);
            }
        });
    }
    }

這里是user類用于定義數(shù)據(jù)的參數(shù)

package com.example.longin_demo.entity;

import java.util.Date;

public class User {
    private int id;
    private String username;
    private String password;
    private String idcard;
    private String name;
    private Date birthdat;
    private String phonenumber;

    public User() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getIdcard() {
        return idcard;
    }

    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthdat() {
        return birthdat;
    }

    public void setBirthdat(Date birthdat) {
        this.birthdat = birthdat;
    }

    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    public User(int id, String username, String password, String idcard, String name, Date birthdat, String phonenumber) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.idcard = idcard;
        this.name = name;
        this.birthdat = birthdat;
        this.phonenumber = phonenumber;
    }
}

dao類用來寫SQL語句的

package com.example.longin_demo.dao;

import android.util.Log;

import androidx.annotation.Nullable;

import com.example.longin_demo.entity.User;
import com.example.longin_demo.untils.JDBCUtils;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserDao {

public boolean register(String username){
    String sql="select * from user1 where username=?";
    Connection con =JDBCUtils.getConn();
    try{
        PreparedStatement pst =con.prepareStatement(sql);
        pst.setString(1,username);
        if(pst.executeQuery().next()){
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        JDBCUtils.close(con);
    }
    return false;

}
    public boolean longin(String username,String password) {
        String sql = "select * from user1 where username=? and password=?";
        Connection con = JDBCUtils.getConn();
        Log.d("UserDao", "連接數(shù)據(jù)庫成功");
        try {
            PreparedStatement pst=con.prepareStatement(sql);
             pst.setString(1,username);
             pst.setString(2,password);


        if (pst.executeQuery().next()){
        return true;
        }

        } catch (SQLException throwables) {
      throwables.printStackTrace();
            Log.d("UserDao", "連接數(shù)據(jù)庫失敗");
        }finally {
        JDBCUtils.close(con);
        }
        return false;
    }


    public boolean register(User user){
        String sql = "insert into user1(username,password,idcard,name,phonenumber) values (?,?,?,?,?)";

        Connection  con = JDBCUtils.getConn();

        try {
            PreparedStatement pst=con.prepareStatement(sql);

            pst.setString(1,user.getUsername());
            pst.setString(2,user.getPassword());
            pst.setString(3,user.getIdcard());
            pst.setString(4,user.getName());
            pst.setString(5, user.getPhonenumber());

            int value = pst.executeUpdate();

            if(value>0){
                return true;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(con);
        }
        return false;
    }
}

JDBC連接數(shù)據(jù)庫

package com.example.longin_demo.untils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCUtils {
    static {
        try{Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public static Connection getConn(){
        Connection  conn = null;
        try {
                conn= DriverManager.getConnection("jdbc:mysql://192.168.1.158:3306/test","root","root");
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn){
        try {
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}

?我還定義了一個類,用來提示信息直接調用彈出提示框,以及報錯,用的定義這個主要是代碼復用率

package com.example.longin_demo.untils;

import android.app.AlertDialog;
import android.content.Context;
import android.widget.Toast;

import java.util.Set;

public class CommonUtils {
    public static void showShortMsg(Context context,String msg){
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }
    public static void showLontMsg(Context context,String msg){
        Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
    }
    public static void showDlgMsg(Context context,String msg){
        new AlertDialog.Builder(context)
                .setTitle("提示信息")
                .setMessage(msg)
                .setPositiveButton("確定",null)
                .setNegativeButton("取消",null)
                .create().show();
    }
}

在AndroidManifest里面一定要加入這一行代碼。

<uses-permission android:name="android.permission.INTERNET" />

下面是通過tcp協(xié)議傳輸?shù)膸讉€類

ConnectThread類

package com.example.longin_demo.tcp;

import android.util.Log;

import com.example.longin_demo.MainActivity2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ConnectThread extends Thread{
    //Socket msg = null;//定義socket
    private OutputStream out_ip=null;//定義輸出流(ip)
    OutputStream outputStream=null;
    private InputStream inputStream;
    private StringBuffer stringBuffer;
    private String ip;
    private int port;
    private Receive receive;
    private String string;
    private boolean isRun = true;
    private  MainActivity2.Receive re;

    public ConnectThread(String ip, int port, MainActivity2.Receive re) {
        this.ip = ip;
        this.port = port;
        this.re = re;
    }


    @Override
    public void run(){
        Socket so = null;
        try {
            so = new Socket(ip, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + ": Hello");
        try {
            inputStream = so.getInputStream();
            out_ip = so.getOutputStream();
            Log.v("AndroidChat","開始連接服務器:"+ip+"/"+port);
            sleep(1000);
        }
        catch (IOException | InterruptedException e) {
            Log.v("AndroidChat","連接服務器失敗"+e.getMessage());
            e.printStackTrace();
            return;
        }
        Log.v("AndroidChat","成功連接上服務器");
            /*
            下面是接收模塊,你可以嘗試探究如何將這個模塊放在接收線程中。
            */

        try {
            inputStream = so.getInputStream();
            final byte[] buffer = new byte[1024];
            final int len = inputStream.read(buffer);
            System.out.println(new String(buffer,0,len));
            Log.v("AndroidChat","接收成功:"+new String(buffer,0,len));
            string = new String(buffer,0,len);
            re.setString(string);
            //上下兩行會和MainActivity關聯(lián),是回調在顯示屏的關鍵步驟。
            MainActivity2.callback();
            System.out.println(new String(buffer,0,len));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Receive類

package com.example.longin_demo.tcp;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.longin_demo.MainActivity2;
import com.example.longin_demo.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.InputStream;
import java.net.Socket;

public class Receive extends Thread {
    private InputStream inputStream;
    private InputStreamReader inputStreamReader;
    private BufferedReader bufferedReader;
    private int port;
    private String ip;
    private String string;
    private MainActivity2 mainActivity;

    public Receive(int port, String ip) {
        this.port = port;
        this.ip = ip;
    }

    @Override
    public void run() {
        try {
            //sleep(1000);
            Socket socket = new Socket(ip,port);
            inputStream = socket.getInputStream();
            final byte[] buffer = new byte[1024];
            final int len = inputStream.read(buffer);
            System.out.println(new String(buffer,0,len));
            Log.v("AndroidChat","接收成功:"+new String(buffer,0,len));
            string = new String(buffer,0,len);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //catch (InterruptedException e) {
        //e.printStackTrace();
        //}

        //}
        /*try {
            Socket socket = new Socket(ip,port);
            socket.shutdownOutput();
            inputStream = socket.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);
            String msg = null;
            msg = bufferedReader.readLine();
            while((msg=bufferedReader.readLine()) != null) {
                inputStreamReader.close();
                Log.v("AndroidChat","服務器說:"+msg);
                System.out.println("服務器說:"+msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    /*new Runnable(){
                    @Override
                    public void run() {
                    }
                };*/
    }
    public class MainActivity extends AppCompatActivity{
        private TextView mTv_recive;
        private Button mBtn_recive;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            mTv_recive = findViewById(R.id.receive);
            mBtn_recive = findViewById(R.id.btn_1);


            mBtn_recive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(),"123",Toast.LENGTH_SHORT).show();
                                    System.out.println(string);
                                }
                            });
                        }
                    }).start();
                }
            });
        }
    }
}

Send類

package com.example.longin_demo.tcp;

import android.util.Log;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Send extends Thread {
    private String send;
    private OutputStream outputStream;
    private InputStream inputStream;
    private String ip;
    private int port;

    public Send(String msg, int port, String ip) {
        this.send = msg;
        this.ip = ip;
        this.port = port;
    }

    @Override
    public void run() {
        try {
            Socket socket = new Socket(ip, port);
            send = "客戶端發(fā)來:" + send;
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
            outputStream.write((send + "\n").getBytes("utf-8"));
            outputStream.flush();
            Log.v("AndroidChat", "發(fā)送成功:" + send);
        } catch (Exception e) {
            Log.v("AndroidChat", "發(fā)送失?。? + send + "error" + e.getMessage());
            e.printStackTrace();
        }

    }
}

登錄后的界面

android項目mysql登錄,android-studio

xlm文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2"
    android:orientation="vertical">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true">
    <EditText
        android:id="@+id/ip"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:background="#7CB342"
        android:hint="@string/ip"
        android:textColor="#ffffff"
        />
    <EditText
        android:id="@+id/port"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="@string/port"
        android:background="#C0CA33"
        android:layout_toEndOf="@id/ip"
        android:inputType="number"
        />
    <Button
        android:id="@+id/connect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/port"
        android:text="@string/connection_server"/>

</RelativeLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        >
        <TextView
            android:id="@+id/receive"
            android:layout_width="match_parent"
            android:layout_height="290dp"
            android:background="#00ACC1"
            android:textColor="#FFFFFF"
            android:hint="@string/receive"
            android:textSize="18sp"
            android:gravity="center_horizontal"
            />
        <EditText
            android:id="@+id/send"
            android:layout_width="match_parent"
            android:layout_height="290dp"
            android:background="#3949AB"
            android:hint="@string/send"
            android:textColor="#ffffff"
            android:layout_below="@id/receive"/>
        <Button
            android:id="@+id/btn_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/send1"
            android:layout_below="@+id/send"
            />

    </RelativeLayout>
<!--    <LinearLayout-->
<!--        android:layout_width="wrap_content"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:gravity="left"-->
<!--    >-->
<!--        <TextView-->
<!--            android:layout_width="410dp"-->
<!--            android:layout_height="match_parent"-->
<!--            android:text="對話窗口"-->
<!--            android:gravity="center"-->
<!--            android:textSize="30dp"/>-->

<!--    </LinearLayout>-->
<!--    <LinearLayout-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="200dp"-->
<!--        android:layout_marginStart="0dp"-->
<!--        android:layout_marginTop="5dp"-->
<!--        android:layout_marginEnd="10dp"-->

<!--        android:gravity="center">-->

<!--        <EditText-->
<!--            android:id="@+id/edit_text"-->
<!--            android:layout_width="fill_parent"-->
<!--            android:layout_height="fill_parent"/>-->
<!--    </LinearLayout>-->



<!--&lt;!&ndash;    <TextView&ndash;&gt;-->
<!--&lt;!&ndash;        android:layout_width="match_parent"&ndash;&gt;-->
<!--&lt;!&ndash;        android:layout_height="wrap_content"&ndash;&gt;-->
<!--&lt;!&ndash;        android:gravity="center"&ndash;&gt;-->
<!--&lt;!&ndash;        android:text="登錄成功"&ndash;&gt;-->
<!--&lt;!&ndash;        android:textSize="25dp"></TextView>&ndash;&gt;-->
<!--    <LinearLayout-->
<!--        android:layout_width="wrap_content"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:gravity="left"-->
<!--        >-->
<!--        <TextView-->
<!--            android:layout_width="410dp"-->
<!--            android:layout_height="match_parent"-->
<!--            android:text="發(fā)送窗口"-->
<!--            android:gravity="center"-->
<!--            android:textSize="30dp"/>-->

<!--    </LinearLayout>-->
<!--    <LinearLayout-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="200dp"-->
<!--        android:layout_marginStart="0dp"-->
<!--        android:layout_marginTop="5dp"-->
<!--        android:layout_marginEnd="10dp"-->

<!--        android:gravity="center">-->

<!--        <EditText-->
<!--            android:id="@+id/edit_text1"-->
<!--            android:layout_width="fill_parent"-->
<!--            android:layout_height="fill_parent"/>-->
<!--    </LinearLayout>-->
<!--<LinearLayout-->
<!--    android:layout_width="match_parent"-->
<!--    android:layout_height="50dp"-->
<!--    >-->
<!--    <Button-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="match_parent"-->
<!--        android:text="發(fā)送"/>-->
<!--</LinearLayout>-->

</RelativeLayout>

MainActivity類

package com.example.longin_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.longin_demo.tcp.ConnectThread;
import com.example.longin_demo.tcp.Send;

public class MainActivity2 extends AppCompatActivity {
    private Button mBtn_send;
    private Button mBtn_connect;
    private EditText mEt_send;
    private static TextView mTv_recv;
    private String ip;
    private int port;
    private String msg;
    private ConnectThread ct;
    private Send send;
    private Handler handler;
    static Receive re = new Receive();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mTv_recv = findViewById(R.id.receive);
        mBtn_connect = findViewById(R.id.connect);
        mBtn_send = findViewById(R.id.btn_1);


        final EditText mEt_ip = this.findViewById(R.id.ip);
        final EditText mEt_port = this.findViewById(R.id.port);
        final EditText mEt_send = this.findViewById(R.id.send);

/*
設置一個點擊事件用以連接線程
*/
        mBtn_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ip = mEt_ip.getText().toString();//獲取輸入的ip
                port = Integer.parseInt(mEt_port.getText().toString());//獲取輸入的端口號

                ct = new ConnectThread(ip, port,re);//創(chuàng)建一個線程來處理消息的收發(fā)
                ct.start();
            }
        });
        /*
設計一個點擊事件用以發(fā)送消息
*/
        mBtn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                msg = mEt_send.getText().toString();
                send = new Send(msg, port, ip);
                send.start();

            }
        });

    }
    /*
    這里是和連接接收線程中的 re.setString(string) 聯(lián)動的,自己理解一下怎么走在腦子里面走一次就差不多了。
    */
    public static class Receive{
        private  String string;
        public String getString(){
            return string;
        }
        public void setString(String s){
            this.string = s;
        }
    }
    /*
    下面的callback和連接接收線程中的 MainActivity.callback() 關聯(lián),線程運行到那一步后結束回到MainActivity中。
    */
    public static void callback(){
        System.out.println("連接線程執(zhí)行結束");
        mTv_recv.setText(re.getString());//這里是將接收到的文字顯示在接收框內。
    }

    }

這個就是大部分的代碼了,我也上傳了demo可以下載demo安裝好環(huán)境是可以直接運行的。有什么不懂的歡迎評論區(qū)留言,多多交流

https://download.csdn.net/download/m0_61438105/87953707?spm=1001.2014.3001.5503

?下載地址,這個有個小BUG每次連接發(fā)送完都需要從新連接一下,有沒有大佬指導一下文章來源地址http://www.zghlxwxcb.cn/news/detail-785888.html

到了這里,關于Android studio 通過mysql連接數(shù)據(jù)庫完成注冊登錄,登陸后通過tcp協(xié)議與電腦的網(wǎng)絡調試助手互發(fā)信息的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • (代碼注釋超詳細)JavaWeb連接Mysql數(shù)據(jù)庫完成登錄注冊業(yè)務

    (代碼注釋超詳細)JavaWeb連接Mysql數(shù)據(jù)庫完成登錄注冊業(yè)務

    登錄:完成連接數(shù)據(jù)庫判斷登陸信息是否有誤 注冊:將信息填寫完畢后點擊注冊,跳轉到登陸頁面 主頁:展示項目信息并且可以在頁面內進行增刪改操作 完整文件目錄如下: 文件目錄: bean包中填寫的代碼為實體類 dao模型就是寫一個類,把訪問數(shù)據(jù)庫的代碼封裝起來 serv

    2023年04月22日
    瀏覽(21)
  • Android Studio + sqllite 數(shù)據(jù)庫連接的步驟以及常見問題

    Android Studio + sqllite 數(shù)據(jù)庫連接的步驟以及常見問題

    軟件見文末 前提是先安裝好sqllite----無腦式next安裝 提示:.xml有些資源需要用自己有的,否者有可能會報錯?。。?! 1、打開SDK后,查看SDK路徑 2、在SDK路徑下右鍵鼠標運行命令行,輸入命令monitor,即可啟動Android monitor Device如下圖所示: 執(zhí)行完monitor,正常情況下會直接跳轉

    2024年02月04日
    瀏覽(32)
  • Android Studio鏈接數(shù)據(jù)庫(MySQL)步驟及問題

    Android Studio鏈接數(shù)據(jù)庫(MySQL)步驟及問題

    1、在libs加入jdbc,右鍵并選擇Add As Library 2、manifests中設置網(wǎng)絡權限。 寫上 3、Android4.0以后版本不支持在主線程中執(zhí)行耗時操作,對數(shù)據(jù)庫的操作要新開一個線程。 簡單示例:(具體應用涉及到線程數(shù)據(jù)傳遞) new Thread(new Runnable() { @Override public void run() { String CLS=“com.mysql.j

    2023年04月13日
    瀏覽(21)
  • Android 連接MySql數(shù)據(jù)庫步驟

    1.首先我們需要導入mysql驅動jar包下載地址: ? 在android studio的build.gradle中插入 } 2、建立JDBCutils類 ? ?分析:jdbc:mysql://192.168.43.215:3306/user\\\",\\\"root\\\",\\\"1234\\\"語句 ? ?1、jdbc:mysql://基本格式 ? ?2、192.168.43.215你當前連的網(wǎng)的Ipv4地址(可以在cmd命令窗口輸入ipconfig命令,找到你連的網(wǎng)(

    2024年02月05日
    瀏覽(23)
  • Android連接MySQL數(shù)據(jù)庫的方法

    Android連接MySQL數(shù)據(jù)庫的方法

    由于之前那篇文章是在大學期間好多東西都不太懂的時候寫的,放到現(xiàn)在再來看總感覺到處都是問題(或者說已經(jīng)看不懂啦)。但是看到每天上漲的那點閱讀量,想了想還是把這個再用相對正常的方式再重寫一遍吧。 為了省事本項目的服務端采用SpringBoot + MybatisPlus開發(fā),要是

    2023年04月08日
    瀏覽(19)
  • Mybatis連接MySQL數(shù)據(jù)庫通過逆向工程簡化開發(fā)流程

    Mybatis連接MySQL數(shù)據(jù)庫通過逆向工程簡化開發(fā)流程

    在開發(fā)中,一般我們的開發(fā)流程是: 數(shù)據(jù)庫:設計數(shù)據(jù)表 實體類:建立與數(shù)據(jù)表對應的pojo實體類 持久層:設計持久層,Mapper接口和Mypper.xml sql映射文件 服務層:添加Service接口和ServiceImpl接口實現(xiàn)類 邏輯控制層:設計各頁面/功能的Cotroller 但是,我們想的是,很多情況下,實

    2024年02月05日
    瀏覽(22)
  • PHP&MySQL基礎(一):創(chuàng)建數(shù)據(jù)庫并通過PHP進行連接

    PHP&MySQL基礎(一):創(chuàng)建數(shù)據(jù)庫并通過PHP進行連接

    PHP同樣可以對數(shù)據(jù)庫進行連接,并且實現(xiàn)增刪改查、登錄注冊等功能,這一篇寫一下怎么使用PHP去連接MySQL數(shù)據(jù)庫 目錄 一、創(chuàng)建數(shù)據(jù)庫 1.1 登錄頁面 1.2 創(chuàng)建數(shù)據(jù)庫 1.3 創(chuàng)建數(shù)據(jù)庫表 1.4 添加表字段 1.5 插入數(shù)據(jù) 1.6?導出和導入 二、PHP連接數(shù)據(jù)庫 2.1 通過 mysqli() 進行實例化 2.

    2024年02月03日
    瀏覽(25)
  • Android:安卓開發(fā)采用Volley網(wǎng)絡框架+MySQL數(shù)據(jù)庫,實現(xiàn)從服務器獲取數(shù)據(jù)并展示完成記單詞APP

    Android:安卓開發(fā)采用Volley網(wǎng)絡框架+MySQL數(shù)據(jù)庫,實現(xiàn)從服務器獲取數(shù)據(jù)并展示完成記單詞APP

    實現(xiàn)功能:設計一個記單詞APP。服務器采用Tomcat,數(shù)據(jù)庫采用Mysql。實現(xiàn)用戶的注冊登錄功能以及單詞的增刪改查。 指標要求:實現(xiàn)UI布局;將系統(tǒng)數(shù)據(jù)保存到Mysql數(shù)據(jù)庫中,并采用Volley網(wǎng)絡框架實現(xiàn)從服務器獲取數(shù)據(jù)并展示。 步驟1:搭建開發(fā)環(huán)境。 步驟2:準備資源。 步驟

    2024年02月13日
    瀏覽(20)
  • ASP.NET WEB API通過SugarSql連接MySQL數(shù)據(jù)庫

    ASP.NET WEB API通過SugarSql連接MySQL數(shù)據(jù)庫

    注意:VS2022企業(yè)版可以,社區(qū)版可能存在問題。實體名稱和字段和數(shù)據(jù)庫中的要一致。 1、創(chuàng)建項目,安裝SqlSugarCore、Pomelo.EntityFrameworkCore.MySql插件 2、文件結構 2、appsettings.json { “Logging”: { “LogLevel”: { “Default”: “Information”, “Microsoft.AspNetCore”: “Warning” } }, “Allowed

    2024年02月12日
    瀏覽(28)
  • Vue項目通過node連接MySQL數(shù)據(jù)庫并實現(xiàn)增刪改查操作

    Vue項目通過node連接MySQL數(shù)據(jù)庫并實現(xiàn)增刪改查操作

    1.創(chuàng)建Vue項目 Vue項目創(chuàng)建的詳細步驟,有需要的可移步這里 2.下載安裝需要的插件 下載express 下載cors,用于處理接口跨域問題 下載mysql 下載axios 3.在項目中創(chuàng)建server文件夾,用于搭建本地服務器 新建/server/app.js,用于配置服務器相關信息 新建/server/db/index.js,用于配置數(shù)據(jù)庫

    2024年02月16日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包