Instantiate ConnectionConfiguration in Smack 4.1 - android

I am implementing a chat feature in my android app. So I have installed an open fire server and Smack Client library and now I have written a code to connect with the server but I am getting an error which states that ConnectionConfiguration is an abstract class.So i cant instaniate. Could you give me some idea about the instantiation of ConnectionConfiguration in SMACK 4.1?

Try to use the example below:
XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setUsernameAndPassword(USER_ID+ "#" + DOMAIN, key);
config.setServiceName(DOMAIN);
config.setHost(DOMAIN);
config.setPort(PORT);
config.setDebuggerEnabled(true);
config.setSocketFactory(SSLSocketFactory.getDefault());
mConnection = new XMPPTCPConnection(config.build());
try {
mConnection.connect();
} catch (SmackException | IOException | XMPPException e) {
e.printStackTrace();
}

Related

Connect to Ejabberd server using Smack on Android emulator

I've started to develop an android chat application using Smack. The server I've been using is Ejabberd 4.2 which I want to connect it on the local machine using the emulator. This is the code to make a connection and print some log text:
try {
InetAddress address = InetAddress.getByName("10.0.2.2");
DomainBareJid serviceName = JidCreate.domainBareFrom("localhost");
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword(user,pass)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setXmppDomain(serviceName)
.setHostAddress(address)
.setPort(5222)
.setDebuggerEnabled(true)
.build();
AbstractXMPPConnection conn = new XMPPTCPConnection(config);
conn.connect();
if (conn.isConnected()){
Log.d(TAG, "connection established");
}
conn.login();
if (conn.isAuthenticated()){
Log.d(TAG, "connection authorized");
}
} catch (Exception e) {
e.printStackTrace();
}
I've used 10.0.2.2 as the localhost domain name for emulator, but it throws ConnectionException.
SmackException$ConnectionException: The following addresses failed:
'null:5222' failed because: /10.0.2.2 exception:
java.net.SocketTimeoutException: failed to connect

How to add sid to a XMPPBOSHConnection?

I'm having trouble connection to my bosh server, it says it needs "sid":
org.igniterealtime.jbosh.BOSHException: Connection Manager session creation response did not include required 'sid' attribute
this is how I'm trying to connect / login:
BOSHConfiguration.Builder configBuilder = BOSHConfiguration.builder();
configBuilder.setUsernameAndPassword(USERNAME, PASSWORD);
configBuilder.setHost(HOST);
configBuilder.setPort(PORT);
configBuilder.setFile(FILE_PATH);
configBuilder.setUseHttps(true);
configBuilder.setServiceName(SERVICE);
AbstractXMPPConnection connection = new XMPPBOSHConnection(configBuilder.build());
try {
connection.connect();
} catch (SmackException e) {
I have same problem and resolved when I added below code :
Bytestream stream = new Bytestream();
stream.setSessionID(username);
configBuilder.setResource(stream.toXML().toString());

Asmack 18 connection : google.com:5222 Exception: Could not connect to talk.google.com remote-server-timeout

I am trying to use asmack 18 to connect to gtlak server for XMPP connection.
public static final String HOST = "talk.google.com";
public static final int PORT = 5222;
public static final String SERVICE = "gmail.com";
ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE);
XMPPConnection connection = new XMPPConnection(connConfig);
try {
//Connect to the server
connection.connect();
connection.login("xxxxxxxx#gmail.com", "password");
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
//xmppClient.setConnection(connection);
Log.d("connection","connection successfull");
} catch (XMPPException ex) {
connection = null;
Log.d("connection","connection fail");
//Unable to connect to server
}
But it gives timeout error.
talk.google.com:5222 Exception: Could not connect to talk.google.com:5222.; : remote-server-timeout(504)
-- caused by: java.net.UnknownHostException: talk.google.com
Read the ReadME =) http://asmack.freakempire.de/0.8.9/README
Static Code
In order to work correctly on Android, you need to register Smack's
XMPP Providers and Extensions manually and init some static code
blocks before you doing any XMPP activty. Calling
SmackAndroid.init(Context) (in org.jivesoftware.smack) will do this
for you.
SmackAndroid.init(getApplicationContext());
ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
...
.

No response from the server error in ejabberd server

Now I am working with XMPP-chat for Android using ejabberd server.
When I am trying to connect to the server, it shows an error. But it works fine in openfire server.
I am using smack library.
Error log is given below:
04-21 20:34:16.824: I/XMPPChatDemoActivity(1929): [SettingsDialog] Connected to 10.0.2.2
04-21 20:34:21.932: E/XMPPChatDemoActivity(1929): Failed to log in as test3#eworks.com
04-21 20:34:21.932: E/XMPPChatDemoActivity(1929): No response from the server.
I found solution how to connect to gtalk and jabber.org with Smack 3.1.0:
Code for GTalk:
ConnectionConfiguration cc = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection connection = new XMPPConnection(cc);
try {
connection.connect();
// You have to put this code before you login
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
// You have to specify your gmail addres WITH #gmail.com at the end
connection.login("some.account#gmail.com", "password", "resource");
// See if you are authenticated
System.out.println(connection.isAuthenticated());
} catch (XMPPException e1) {
e1.printStackTrace();
}
For jabber.org here is the code:
ConnectionConfiguration cc = new ConnectionConfiguration("jabber.org", 5222, "jabber.org");
XMPPConnection connection = new XMPPConnection(cc);
try {
connection.connect();
// You have to put this code before you login
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
// You have to specify your Jabber ID addres WITHOUT #jabber.org at the end
connection.login("your.jabber", "password", "resource");
// See if you are authenticated
System.out.println(connection.isAuthenticated());
} catch (XMPPException e1) {
e1.printStackTrace();
}
With this code i can now connect to my local ejabberd and openfire server. I hope this will solve your problems.

Keep XMPP connection(using smack) alive throughout application

I am using the XMPP Connection(using smack) for chat in android application.I have made the connection with openfire and also i can send and receive the message.But the problem is that when i go in the XMPPClient.java activity then it made the connection.So i cant get any message till not go in that activity.So how can made the connection at the starting and then reuse at other activity.Code is in this 2 links ConnectionSettings file and the chatscreen in which we can do chat.In this link the comment line is also my questions so please also see that comment.
Create global XMPPConnection object and Use below funciton and store in global XMPPConnection object and use that connection object everywhere. This is a sample gtalk example.
public XMPPConnection login() throws XMPPException {
ConnectionConfiguration config = new
ConnectionConfiguration("talk.google.com",5222,"gmail.com");
config.setSecurityMode(SecurityMode.required);
config.setTruststoreType("BKS");
config.setTruststorePath("/system/etc/security/cacerts.bks");
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
connection.login(username, password);
Presence presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.available);
connection.sendPacket(presence);
try {
Thread.sleep(3000);
} catch (Exception ex) {
ex.printStackTrace();
}
return connection;
}

Categories

Resources