使用Smack:login(“foo”,“bar”)给出“Not connected to server”异常

时间:2015-07-27 07:03:35

标签: xmpp smack

我很抱歉这个noob问题,在我的应用程序中我使用一个按钮发送朋友请求,另一个人接受它并且它工作正常,我的问题是,当我想删除一个联系我得到致命EXCEPTION:main java.lang.IllegalStateException:未连接到服务器。

错误在此行

connection.login("113", "AA");

这是我的代码,我不知道这段代码是否有效:(有人可以帮帮我吗?

 ConnectionConfiguration connConfig = new ConnectionConfiguration(
                "xxx.xxx.x.xx", 5222, "xxx.xxx.x.xx");
        XMPPConnection connection = new XMPPConnection(connConfig);

        SharedPreferences phoneNo3 = this.getSharedPreferences("phonef", MODE_PRIVATE);
        String  username = phoneNo3.getString("phonef", "");
        SharedPreferences conts = this.getSharedPreferences("contrass", MODE_PRIVATE);
        String  passwords = conts.getString("contrass", "");

        try {
        connection.login("113", "AA");

        RosterPacket packet = new RosterPacket();
        packet.setType(IQ.Type.SET);
        RosterPacket.Item item  = new RosterPacket.Item("laboral@xxx.xxx.x.xx", null);
        item.setItemType(RosterPacket.ItemType.remove);
        packet.addRosterItem(item);
        connection.sendPacket(packet);

        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
           connection.disconnect();
        }

1 个答案:

答案 0 :(得分:2)

您需要在connect()之前致电login(String, String)