1
votes

I'm trying to connect to Gtalk from my Android app using Smack 4.1, but everytime I tried to connect, it shows error : javax.net.ssl.SSLHandshakeException: Handshake failed

Below is my code :

private class ConnectionThread extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {
            boolean isConnected = false;
            XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
                    .builder();

            config.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
            config.setUsernameAndPassword(username,password);
            config.setServiceName(service);
            config.setHost(host);
            config.setPort(port);
            config.setDebuggerEnabled(true);
            //config.setCompressionEnabled(false);
            config.setSocketFactory(SSLSocketFactory.getDefault());

            connection = new XMPPTCPConnection(config.build());

            XMPPConnectionListener connectionListener = new XMPPConnectionListener();
            connection.addConnectionListener(connectionListener);

            try {
                connection.connect();

                isConnected = true;
            } catch (Exception e) {
                Log.e(TAG,"Unable to connect to server = " + e.toString());
            }
            return isConnected;
        }

    }

Where host,username, password service and port coming from String.xml values :

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">XmppLab</string>
<string name="action_settings">Settings</string>
<string name="connect">Connect</string>
<string name="host">talk.google.com</string>
<string name="port">5222</string>
<string name="username">[email protected]</string>
<string name="password">password</string>
 <string name="service">gmail.com</string>

Kindly advise do I missed something here? And really appreciate for any kind help.

1
config.setSocketFactory(SSLSocketFactory.getDefault()); Why?Flow
Hi Flow, thanks for the hint. I share my edited code below and hope it will help others with same problem.Vierda Mila Nartila
@Flow i get ExceptionInInitializeError when i try to initialize XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration .builder(); is this behaviour known ??Sharp Edge

1 Answers

1
votes

Thanks for the hint. I change my code as below and now it can connect to Gtalk :

config.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
            config.setUsernameAndPassword(username,password);
            config.setServiceName(service);
            config.setHost(host);
            config.setPort(port);
            config.setDebuggerEnabled(true);
            //config.setCompressionEnabled(false);
            //config.setSocketFactory(SSLSocketFactory.getDefault());