Can't establish a new connection with aSmack 4.0.2

前端 未结 2 445
梦毁少年i
梦毁少年i 2021-01-23 02:08

I\'m learning Android programming and I\'ve been trying to figure this out for a couple days now. I\'m writing and Android app that is supposed to connect to XMPP server. I\'m g

2条回答
  •  死守一世寂寞
    2021-01-23 02:16

    I had the same problem, but found solution here: SImple Asmack program not working

    The solution is to put connection code into separate thread.

    public static final String HOST = "208.68.163.218"; //write your host name
    public static final int PORT = 5222;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connect();
    }
    
    public void connect() {
        Thread t = new Thread(new Runnable() {
    
            @Override
            public void run() {
                Context context = getApplicationContext();
                SmackAndroid.init(context);
                ConnectionConfiguration ConnectionConfiguration = new ConnectionConfiguration(HOST, PORT);
                ConnectionConfiguration.setDebuggerEnabled(true);
                XMPPConnection connection = new XMPPTCPConnection(ConnectionConfiguration);     
    
                try {
                    connection.connect();
                } catch (ConnectionException e) {
                    e.printStackTrace();
                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }
    

提交回复
热议问题