使用Smack库登录
①android studio添加smack库依赖
repositories { maven { url 'https://oss.sonatype.org/content/repositories/snapshots' } mavenCentral() } dependencies { compile "org.igniterealtime.smack:smack-android:4.3.0" compile "org.igniterealtime.smack:smack-tcp:4.3.0" } configurations { all*.exclude group: 'xpp3', module: 'xpp3' }
(
我试过下载smack的完整jar包,再手动添加,出现很多缺少第三方依赖库的情况;
再有试过添加smack4.2版本的依赖,报空指针异常
)
②登录
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) login("admin","yourpassword") } fun login(username:String,password:String){ var addr:InetAddress = InetAddress.getByName("169.254.235.122") val config: XMPPTCPConnectionConfiguration = XMPPTCPConnectionConfiguration.builder() .setUsernameAndPassword(username,password) .setXmppDomain("169.254.235.122") .setHost("169.254.235.122") .setHostAddress(addr) .setPort(5222) .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) // .setSendPresence(true) // .setDebuggerEnabled(true) .build() Thread{ kotlin.run { val conn:XMPPTCPConnection = XMPPTCPConnection(config) try{ conn.connect().login() println("登录成功...") }catch (e:Exception){ println("登录错误...") e.printStackTrace() } } }.start() } }
说明:
openfire相关配置
参考
Android Studio 使用 smack4.3 搭建 Openfire+smack 开源IM开发环境
错误提示:
org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: '169.254.235.122:5222' failed because: org.minidns.hla.ResolutionUnsuccessfulException: Asking for 169.254.235.122. IN A yielded an error response NX_DOMAIN,
解决方案:
error-on-smack-4-2-0-in-aaaa-yielded-an-error-response-nx-domain
错误提示:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
...
解决方案:
设置安全模式:
setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
错误提示:
org.jivesoftware.smack.sasl.SASLErrorException: SASLError using SCRAM-SHA-1: not-authorized
原因:
大概就是密码错误