我在使用smack 4.1.0-beta2连接到我的ejabberd 2.1.11服务器时遇到问题。我认为它与SSL证书有关,并且不想处理那些我只是在没有启动TLS的情况下运行ejabberd的东西。我知道那是在工作bc我能够使用psi连接到ejabberd而不会警告我一些自签名证书。
无论如何,我正在使用以下ConnectionConfiguration的
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword("username", "password");
configBuilder.setServiceName("myapp.ddns.net");
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
configBuilder.setCompressionEnabled(false);
AbstractXMPPConnection xmppConnection = new XMPPTCPConnection(configBuilder.build());
所以当我尝试xmppConnection.connect()时,我得到以下异常
02-05 01:01:42.965 17047-17047/net.ddns.myapp W/DNSUtil﹕ Exception while resovling SRV records for myapp.ddns.net. Consider adding '_xmpp-(server|client)._tcp' DNS SRV Records
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175)
at libcore.io.IoBridge.sendto(IoBridge.java:463)
at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
at java.net.DatagramSocket.send(DatagramSocket.java:287)
at de.measite.minidns.Client.query(Client.java:199)
at de.measite.minidns.Client.query(Client.java:174)
at de.measite.minidns.Client.query(Client.java:236)
at de.measite.minidns.Client.query(Client.java:149)
at org.jivesoftware.smack.util.dns.minidns.MiniDnsResolver.lookupSRVRecords(MiniDnsResolver.java:79)
at org.jivesoftware.smack.util.DNSUtil.resolveDomain(DNSUtil.java:162)
at org.jivesoftware.smack.util.DNSUtil.resolveXMPPDomain(DNSUtil.java:119)
at org.jivesoftware.smack.AbstractXMPPConnection.populateHostAddresses(AbstractXMPPConnection.java:551)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:506)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:810)
at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:355)
at net.ddns.myapp.MainActivity.onCreate(MainActivity.java:61)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
以及
02-05 01:01:42.980 17047-17047/net.ddns.myapp E/MyApp﹕ Error while establishing XMPPConnection
org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: myapp.ddns.net:5222 Exception: null
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:551)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:810)
at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:355)
at net.ddns.myapp.MainActivity.onCreate(MainActivity.java:61)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
对于android.os.NetworkOnMainThreadException,您需要
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);}
您也可以尝试设置主机值
configBuilder.setHost("myapp.ddns.net");
答案 1 :(得分:0)
我错了,因为我不知道StrictMode.ThreadPolicy,与Smack或ejabberd没什么直接关系,这是基本的网络。
找到答案here