我买了一台华为虚拟主机,系统是Linux,并购买了域名,网站也备案了,域名为:android666.cn。
然后在这台虚拟主机上安装Openfire,安装教程可参考:https://www.cdsy.xyz/computer/system/linux/230205/cd40234.html
安装后浏览器打开http://android666.cn:9090即可打开管理后台,我的配置如下:
我把服务器名称和主机名都设置为:android666.cn,即我的网站域名。
注册很简单,打开Spark,点击左下角的“账号”按钮进入注册界面,如下:
输入账号、密码和服务器地址,点击“创建账号”即可。
账号创建后即可进行登录,如下:
在“高级”设置中无需进行任何设置即可进行登录,一切都用默认设置即可,如下:
登录成功后,效果如下:
官方使用文档:https://download.igniterealtime.org/smack/docs/latest/documentation/
Smack依赖设置:https://github.com/igniterealtime/Smack/wiki/Smack-4.4-Readme-and-Upgrade-Guide
根项目的build.gradle:
allprojects {
repositories {
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
mavenCentral()
}
}
Module的build.gradle:
dependencies {
implementation "org.igniterealtime.smack:smack-android-extensions:4.4.0"
implementation "org.igniterealtime.smack:smack-tcp:4.4.0"
}
configurations {
all*.exclude group: 'xpp3', module: 'xpp3'
}
权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
SDK初始化,可放到Appliation中进行:
AndroidSmackInitializer.initialize(this)
配置好这些之后,就可以开始写代码了,官方教程中演示了一个登录的示例,如下:
private fun login() {
thread {
try {
val connection = XMPPTCPConnection("test001", "123456", "android666.cn")
connection.connect().login()
toast("登录成功")
} catch (e: Exception) {
Log.e(tag, "登录失败", e)
toast("登录失败")
}
}
}
fun toast(text: CharSequence) {
if (Looper.myLooper() == Looper.getMainLooper()) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
} else {
runOnUiThread {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
}
}
登录代码是很简单,但是登录时失败的,报错如下:
2021-03-08 15:50:10.499 13630-13727/cn.android666.imdemo E/MainActivity: 登录失败
org.jivesoftware.smack.SmackException$SmackWrappedException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
at org.jivesoftware.smack.AbstractXMPPConnection.setCurrentConnectionExceptionAndNotify(AbstractXMPPConnection.java:690)
at org.jivesoftware.smack.AbstractXMPPConnection.notifyConnectionError(AbstractXMPPConnection.java:994)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.access$4100(XMPPTCPConnection.java:130)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1152)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$700(XMPPTCPConnection.java:913)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:936)
at java.lang.Thread.run(Thread.java:929)
这里我并没有把异常贴完整,但也足够了解原因了,关键点:CertPathValidatorException: Trust anchor for certification path not found. 这一看就知道是证书的问题,我们并没有为Openfire配置证书,但是默认登录时它会使用证书配置进行登录,所以报错了。所以解决方案就是在登录时,让他不要使用证书,如下:
private fun login() {
thread {
try {
val config = XMPPTCPConnectionConfiguration
.builder()
.setUsernameAndPassword("test001", "123456")
.setXmppDomain(JidCreate.domainBareFrom("android666.cn"))
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) // 禁用安全模式
.build()
val connection = XMPPTCPConnection(config)
connection.connect().login()
toast("登录成功")
} catch (e: Exception) {
Log.e(tag, "登录失败", e)
toast("登录失败")
}
}
再次登录,就可以登录成功了!