【问题标题】:ConnectionException when trying to connect to XMPP server from aSmack Android client尝试从 aSmack Android 客户端连接到 XMPP 服务器时出现 ConnectionException
【发布时间】:2014-11-17 20:45:40
【问题描述】:

我已经尝试从我的 aSmack Android 客户端连接到我的(本地托管的)Openfire XMPP 服务器几个小时了,但它仍然无法正常工作。

我得到一个org.jivesoftware.smack.SmackException$ConnectionException,就是这样。

代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SmackAndroid.init(getApplicationContext());
    connect();
}

private boolean connect(){
    XMPPConnection connection = new XMPPTCPConnection(HOST);
    try{
        connection.connect();
        connection.login("user", "user");
    }catch (Exception e){
        e.printStackTrace();
    }
    return true;
}

服务器已启动并正在运行。

主机是我的服务器名称,也尝试了我的主机名,尝试了不同的端口... 还尝试从另一个线程启动 connect() 方法。 尝试使用登录或匿名连接,但在此之前抛出异常,在行:connection.connect();

非常感谢任何帮助。

【问题讨论】:

    标签: xmpp smack asmack


    【解决方案1】:

    我得到一个 org.jivesoftware.smack.SmackException$ConnectionException 和 就是这样。

    不,不是。如果你看javadoc for ConnectionException

    如果 Smack 无法连接到所有 给定 XMPP 服务的主机。可以使用以下命令检索失败的主机 getFailedAddresses(),这将导致异常 连接失败设置和检索 HostAddress.getException()。

    所以调用ConnectionException.getFailedAddresses() 来检索列表并与HostAddress.getException() 一起检查导致Smack 无法连接到主机的原因。

    【讨论】:

    • 好的。所以首先出现了 NetworkOperationOnMainThread 异常。这是固定的,但现在我得到一个 UnknownHostException: Host is unresolved。尝试了 Openfire 指示的主机/服务器名称,127.0.0.1,...我的服务器运行正常,正常运行时间正在增加。谢谢你的帮助,Flow。
    • 127.0.0.1 不对,它是您的模拟器或智能手机的本地主机。如果你使用模拟器,试试这个地址 10.0.2.2,更多信息在这里developer.android.com/tools/devices/…
    • 是 connecton.getfailedaddresses() 还是 exception.getfailedaddresses() 似乎都不适合我。我正在使用 XMPPTCPConnection。
    【解决方案2】:

    由于无法在Android主线程上进行网络操作而引发异常,您应该将其移至另一个线程。

    【讨论】:

      【解决方案3】:
      **XMPPTCPConnection connection is already configured so please check the connection before creating object of XMPPTCPConnection class.**
      
      XMPPTCPConnection  connection;
       if (connection != null) {
      connection = null;//make it null 
        }
      connection = new XMPPTCPConnection(config.build());
      
      I am getting this same problem but now it has been fixed try it is working.
      

      【讨论】:

        【解决方案4】:

        试试下面的代码。它对我有用:

        AndroidConnectionConfiguration config = new AndroidConnectionConfiguration(HOST);
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        XMPPConnection connection = new XMPPConnection(config);
        

        然后连接并登录

        【讨论】:

        • 不,仍然得到相同的异常。顺便说一句,我使用 ConnectionConfiguration 和 XMPPTCPConnection 对象。使用 asmack-android-8-4.0.4 库版本。
        • 很遗憾,我无法为您提供更多帮助。对不起,伙计。
        • 没关系,我会想办法的。还是谢谢!
        • @TheHungryAndroider 我在将 android 虚拟设备连接到 xmpp 服务器时遇到问题。我正在使用这个code 连接到xmpp,我得到exception,你能告诉任何解决方案吗?
        • @TheHungryAndroider 感谢您的关注,通过this 解决方案解决了
        【解决方案5】:

        尝试添加

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

        到您的清单。因为您的应用程序无法连接到 Internet。

        【讨论】:

          猜你喜欢
          • 2012-06-14
          • 2021-06-22
          • 2014-11-04
          • 2013-08-27
          • 2022-01-20
          • 1970-01-01
          • 2014-10-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多