Android Studio和阿里云數(shù)據(jù)庫實現(xiàn)一個遠(yuǎn)程聊天程序
沒有阿里云數(shù)據(jù)庫的可以買個最便宜的,我是新用戶9.9元買了一個
1.買到后點擊左上角的工作臺

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

開始寫Android Studio項目代碼了,先來看看我的項目結(jié)構(gòu)

依賴包下載地址 Central Repository: mysql/mysql-connector-java (maven.org)
我第一次下了個版本比較新的發(fā)現(xiàn)會報錯,由于我能力有限,所以就老實下載一個低版本的
添加依賴包應(yīng)該都會了吧,不要忘了添加后還要添加到模塊

MainActivity代碼如下
注意代碼里涉及SQL語句,這里要根據(jù)你之前新建的數(shù)據(jù)庫和新建的表來寫,我新建的表是test
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
private TextView t1; //用于顯示獲取的信息
private Button sendmsg; //發(fā)送消息按鈕
private EditText et_msg;//用戶輸入信息框
private String user="user"; //默認(rèn)用戶昵稱
private boolean T=false;//發(fā)送標(biāo)志位
//數(shù)據(jù)庫連接類對象
private static Connection con = null;
private static PreparedStatement stmt = null;
private Button revise;
private EditText et_revise;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
t1 = findViewById(R.id.t1);
et_msg = findViewById(R.id.msg);
et_revise = findViewById(R.id.reviseText);
sendmsg = findViewById(R.id.button);
revise = findViewById(R.id.revise);
revise.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
user = et_revise.getText().toString();
Toast.makeText(MainActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
}
});
sendmsg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { T=true; }
});
//TODO 啟動發(fā)送線程,用按鈕控制發(fā)送標(biāo)志位T,來進(jìn)行發(fā)送信息【注意:連接數(shù)據(jù)庫必須在線程內(nèi),不然會報錯】
Threads_sendmsg threads_sendmsg = new Threads_sendmsg();
threads_sendmsg.start();
//TODO 啟動獲取數(shù)據(jù)線程,讀取數(shù)據(jù)庫里的信息【注意:連接數(shù)據(jù)庫必須在線程內(nèi),不然會報錯】
Threads_readSQL threads_readSQL = new Threads_readSQL();
threads_readSQL.start();
}
class Threads_sendmsg extends Thread {
@Override
public void run() {
while (true){
while (T){
try {
con = MySQLConnections.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
try {
//注意你數(shù)據(jù)庫中是否有 test 這個表,我新建的表是 test
//還有我的屬性,是否和我一樣呢,不一樣就按你自己的來吧
String msg =et_msg.getText().toString().trim(); //用戶發(fā)送的信息
if (msg.isEmpty()){
Looper.prepare();
Toast.makeText(MainActivity.this, "消息為空", Toast.LENGTH_SHORT).show();
Looper.loop();
T=false;
break;
}
if (msg.length()>199){
Looper.prepare();
Toast.makeText(MainActivity.this, "消息長度超過限制", Toast.LENGTH_SHORT).show();
Looper.loop();
T=false;
break;
}
if (con!=null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "發(fā)送成功", Toast.LENGTH_SHORT).show();
}
});
String sql = "insert into test(name,msg,sign) values(?,?,?)";
stmt = con.prepareStatement(sql);
// 關(guān)閉事務(wù)自動提交 ,這一行必須加上
con.setAutoCommit(false);
stmt.setString(1,user);
stmt.setString(2,msg);
stmt.setInt(3,1);
stmt.addBatch();
stmt.executeBatch();
con.commit();
}
}catch (SQLException e){
System.out.println(e);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"請輸入正確的語句",Toast.LENGTH_SHORT).show();
}
});
}
T=false;
}
}
}
}
class Threads_readSQL extends Thread {
ResultSet rs;
@Override
public void run() {
while (true) {
try {
con = MySQLConnections.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
try {
//注意你數(shù)據(jù)庫中是否有 test 這個表,我新建的表是 test
//還有我的屬性,是否和我一樣呢,不一樣就按你自己的來吧
String sql = "select name,msg,sign from test";
if (con != null) {
stmt = con.prepareStatement(sql);
// 關(guān)閉事務(wù)自動提交
con.setAutoCommit(false);
rs = stmt.executeQuery();//創(chuàng)建數(shù)據(jù)對象
//清空上次發(fā)送的信息
t1.setText(null);
while (rs.next() ) {
t1.append(rs.getString(1) + "\n" + rs.getString(2) + "\n\n");
}
con.commit();
rs.close();
stmt.close();
}
//2秒更新一次
sleep(2000);
} catch (Exception e) {
System.out.println(e);
runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(MainActivity.this,"請輸入正確的語句",Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
}
MainActivity布局文件activity_main.xml
<?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=".MainActivity"
android:orientation="vertical">
// An highlighted block
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:text="Hello World!"
android:layout_marginLeft="8dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/reviseText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="請輸入你的昵稱"
android:inputType="textPersonName" />
<Button
android:id="@+id/revise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="修改" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:hint="請輸入內(nèi)容"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="發(fā)送" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MYSQLConnections代碼如下
注意我寫的注釋,用戶名,密碼,外網(wǎng)地址,外網(wǎng)端口號,數(shù)據(jù)庫名稱,這些都要填寫你自己的
import java.sql.Connection;
import java.sql.DriverManager;
public class MySQLConnections {
private String driver = "";
private String dbURL = "";
private String user = "";
private String password = "";
private static MySQLConnections connection = null;
private MySQLConnections() throws Exception {
driver = "com.mysql.jdbc.Driver"; //這里根據(jù)你下載的依賴包版本會有不同的寫法,下載低版本的就是這樣寫
//rm-bp1lxt0mjpf6o.mysql.rds.aliyuncs.com:3306 這個是外網(wǎng)地址,3306是外網(wǎng)端口號,這些都需要填寫你自己的
//sqlconsole 這個是你登錄你的數(shù)據(jù)庫后新建的數(shù)據(jù)庫(應(yīng)該不繞口吧)
dbURL = "jdbc:mysql://rm-bp1lxt0m.mysql.rds.aliyuncs.com:3306/sqlconsole";
user = "user"; //你新建庫時的用戶名
password = "123456"; //你新建庫時的密碼,這里我就不寫我的真密碼了
System.out.println("dbURL:" + dbURL);
}
public static Connection getConnection() {
Connection conn = null;
if (connection == null) {
try {
connection = new MySQLConnections();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
try {
Class.forName(connection.driver);
conn = DriverManager.getConnection(connection.dbURL,
connection.user, connection.password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
AndroidManifest.xml
注意這里要添加網(wǎng)絡(luò)請求權(quán)限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mysqlconnections">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication1">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最后看我運(yùn)行結(jié)果
參考博客
Android Studio 連接阿里云數(shù)據(jù)庫【制作基于數(shù)據(jù)庫的多人遠(yuǎn)程聊天APP】
到此這篇關(guān)于Android Studio和阿里云數(shù)據(jù)庫實現(xiàn)一個遠(yuǎn)程聊天程序的文章就介紹到這了,更多相關(guān)Android Studio阿里云遠(yuǎn)程聊天內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- android Socket實現(xiàn)簡單聊天功能以及文件傳輸
- Android 獲取應(yīng)用簽名的實現(xiàn)
- android 仿微信聊天氣泡效果實現(xiàn)思路
- Android 應(yīng)用APP加入聊天功能
- Android如何獲取QQ與微信的聊天記錄并保存到數(shù)據(jù)庫詳解
- 詳解Android 獲取手機(jī)中微信聊天記錄方法
- Android藍(lán)牙通信聊天實現(xiàn)發(fā)送和接受功能
- Android中基于XMPP協(xié)議實現(xiàn)IM聊天程序與多人聊天室
- Android實現(xiàn)聊天界面
- Android?Studio實現(xiàn)智能聊天
相關(guān)文章
Android網(wǎng)絡(luò)工具類NetworkUtils詳解
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)工具類NetworkUtils,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Android編程入門之HelloWorld項目目錄結(jié)構(gòu)分析
這篇文章主要介紹了Android編程入門之HelloWorld項目目錄結(jié)構(gòu)分析,較為詳細(xì)的分析了Android項目的目錄結(jié)構(gòu)與具體作用,需要的朋友可以參考下2015-12-12
Android實現(xiàn)機(jī)房座位預(yù)約系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)機(jī)房座位預(yù)約系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Android 圖片縮放與旋轉(zhuǎn)的實現(xiàn)詳解
本篇文章是對在Android中實現(xiàn)圖片縮放與旋轉(zhuǎn)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android開發(fā)筆記之:Splash的實現(xiàn)詳解
本篇文章是對Android中Splash的實現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android?通過productFlavors實現(xiàn)多渠道打包方法示例
這篇文章主要為大家介紹了Android?通過productFlavors實現(xiàn)多渠道打包方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android自定義View實現(xiàn)繪制虛線的方法詳解
這篇文章主要給大家介紹了Android自定義View實現(xiàn)繪制虛線的方法,在繪制過程中走了一些彎路才實現(xiàn)了虛線的效果,所以想著總結(jié)分享出來,方便有需要的朋友和自己在需要的時候參考學(xué)習(xí),下面來一起看看吧。2017-04-04

