使用Smack 4.1连接到Gtalk

时间:2015-04-07 08:46:05

标签: android smack google-talk

我正在尝试使用Smack 4.1从我的Android应用程序连接到Gtalk,但每次我尝试连接时,都会显示错误:javax.net.ssl.SSLHandshakeException:握手失败

以下是我的代码:

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;
        }

    }

来自String.xml值的主机,用户名,密码服务和端口:

<?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">username@gmail.com</string>
<string name="password">password</string>
 <string name="service">gmail.com</string>

请告知我在这里遗漏了什么?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

感谢您的提示。我更改了我的代码,现在它可以连接到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());