简体   繁体   中英

Error while trying to connect openfire using smack on android

XMPPTCPConnectionConfiguration.Builder configBuilder =  XMPPTCPConnectionConfiguration.builder();
    configBuilder.setUsernameAndPassword("test", "test");
    configBuilder.setResource("test");
    configBuilder.setServiceName("37.139.26.142");
    configBuilder.setHost("37.139.26.142");
    configBuilder.setPort(5222);
    configBuilder.setSendPresence(true);
    configBuilder.setDebuggerEnabled(true);
    configBuilder.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.required );
    SASLMechanism mechanism = new SASLDigestMD5Mechanism();
    SASLAuthentication.registerSASLMechanism(mechanism);
    SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
    SASLAuthentication.unBlacklistSASLMechanism("DIGEST-MD5");
    AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
    try {
        connection.connect();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    try {
        connection.login();
    } catch (XMPPException e) {
        e.printStackTrace();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

I'm currently trying to handshake my android application and my openfire server(working on ubuntu). But I couldnt. I dont get any fail or something. Just nothing happens. And that feels bad.

Did you try to send a message? Are you sure that you are not connected? Did you check on Openfire admin that your test user is not connected?

First I suggest you to try to send a message:

ChatManager chatmanager = ChatManager.getInstanceFor(connection);
Chat newChat = chatmanager.createChat("anotheruser@yourdomain", new MessageListener() {
    public void processMessage(Chat chat, Message message) {
        System.out.println("Received message: " + message);
    }
});

try {
    newChat.sendMessage("Howdy!");
}
catch (XMPPException e) {
    System.out.println("Error Delivering block");
}

I got this code from : http://www.igniterealtime.org/builds/smack/docs/latest/documentation/messaging.html

Another suggestion is to disable the SecurityMode, just for a test.

configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);

If nothing of this works, try to use the configuration below, which works for me.

XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
                        .builder();
                config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
                config.setServiceName(serverAddress);
                config.setHost(serverAddress);
                config.setPort(5222);
                config.setDebuggerEnabled(true);
        connection = new XMPPTCPConnection(config.build());

        try {
            connection.connect();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }
        try {
            connection.login(loginUser, passwordUser);
        } catch (XMPPException e) {
            e.printStackTrace();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

First I find out this is not about Android part, this is about Openfire part. Because I couldnt connect it with Spark and I saw this in Logcat;

W/System.err: org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: '37.139.26.142:5222' failed because java.net.SocketTimeoutException: failed to connect to /37.139.26.142 (port 5222) after 30000ms

Then I did some research and tried somethings and I saw it is about Ubuntu(at least for me). Then I moved my openfire server to Centos. Then I could be able to connect with Spark to it. Then I got another problem.

org.jivesoftware.smack.SmackException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

And I solved this problem with this code below. I hope this can help others.

public class MainActivity extends AppCompatActivity {

@Override


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    SmackConfiguration.DEBUG = true;
    XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
    configBuilder.setUsernameAndPassword("test", "test");
    configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    configBuilder.setResource("test");
    configBuilder.setServiceName("enter your server ip here");
    configBuilder.setHost("eneter your server ip here");
    configBuilder.setPort(5222);
    configBuilder.setSendPresence(true);
    configBuilder.setDebuggerEnabled(true);
    SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
    SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
    SASLAuthentication.unBlacklistSASLMechanism("PLAIN");

    XMPPTCPConnection connection;
    connection = new XMPPTCPConnection(configBuilder.build());
    // Connect to the server
    try {
        connection.connect();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    // Log into the server

    try {
        connection.login();
    } catch (XMPPException e) {
        e.printStackTrace();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM