黄从杰
黄从杰

Reputation: 1

how to use smack 4.1 in android studio?

As project manager's requirement, we need use https://github.com/igniterealtime/Smack for xmpp connection instead of the old one aSmack library. But as I found, the new xmpp library igniterealtime is not friendly for android studio IDE user, after I used this library in our project, I cannot get it connected to our xmpp server.

Question 1: how to make this new library works?

Question 2: how to send info query packet to xmpp server, in other words how to send "<iq>...</iq>" to server to query some information?

Upvotes: 0

Views: 640

Answers (1)

黄从杰
黄从杰

Reputation: 1

Regards question 1: first, change app level build.gradle:

add following to your dependencies segment: compile ("org.igniterealtime.smack:smack-android:4.1.0") { exclude group: 'xpp3', module: 'xpp3' } // Optional for XMPPTCPConnection compile ("org.igniterealtime.smack:smack-tcp:4.1.0") { exclude group: 'xpp3', module: 'xpp3' } // Optional for XMPP-IM (RFC 6121) support (Roster, Threaded Chats, …) compile ("org.igniterealtime.smack:smack-im:4.1.0") { exclude group: 'xpp3', module: 'xpp3' }

add following to your repositories segment: maven { url 'https://oss.sonatype.org/content/repositories/snapshots' } mavenCentral()

Already include igniterealtime xmpp library to project, we can use it for now.

    XMPPTCPConnectionConfiguration cfg = XMPPTCPConnectionConfiguration.builder()
    .setServiceName("online.yourdomain.com")
    .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
    .setCompressionEnabled(false)
    .setDebuggerEnabled(true)
    .build();

    XMPPTCPConnection xmppConnection = new XMPPTCPConnection(cfg);
    xmppConnection.connect();

    if (!xmppConnection.isAuthenticated()) {
        try {
            xmppConnection.login(jabberId, yourSessionStr, resourceID);
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Regards question 2:

    String XMPP_NAMESPACE = "myapp:notification";
    SimpleIQ iq = new SimpleIQ("query", XMPP_NAMESPACE) {
    @Override
    protected IQChildElementXmlStringBuilder   getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
        xml.attribute("action", "list");
        xml.rightAngleBracket();
        xml.element("from", lastDisconnectedTime + "");
        return xml;
    }
    };
    iq.setType(IQ.Type.get);
    try {
        xmppConnection.sendPacket(iq);
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    }

ps: I found send IQ packet is not as convenient as using asmack. here is the old way for sending IQ packet.

    IQ iq = new IQ() {
        @Override
        public String getChildElementXML() {
            String query = "<query xmlns=\'" + XMPP_NAMESPACE + "\' action=\'list\'>";
            query += "<from>" + lastDisconnectedTime + "</from>";
            query += "</query>";
            return query;
        }
    };

    iq.setType(Type.GET);
    xmppConnection.sendPacket(iq);

notice: I have to disable the security mode when creating XMPPTCPConnectionConfiguration, otherwise, cannot establish the xmpp connection. if you want to make this xmpp connect secure, you have to create your own bks certification file first, then use it in your xmpp connection.

Upvotes: 0

Related Questions