// Create connection object first
// refer to http://codepad.co/s/6fb674 to create config
XMPPConnection.DEBUG_ENABLED = true;
XMPPConnection connection = new XMPPConnection(config);
// before connection established, we better put some listener
// like below
IncomingPacketListener incomingPacketListener = new IncomingPacketListener();
OutgoingPacketListener outgoingPacketListener = new OutgoingPacketListener();
connection.addPacketListener(incomingPacketListener, new PacketFilter() {
@Override
public boolean accept(Packet packet) {
return true; // ensure to listen all kind of incoming packet
}
});
connection.addPacketInterceptor(outgoingPacketListener, new PacketFilter() {
@Override
public boolean accept(Packet packet) {
return true; // ensure to listen all kind of outgoing packet
}
});
connection.addConnectionListener(connectionListener);
// Connect it to server
try {
smackAndroid = SmackAndroid.init(context);
} catch(IllegalStateException e){
Log.e("Connecting", "IllegalStateException: "+e.getMessage());
}
try {
connection.connect();
// connection established here
} catch (XMPPException e) {
Log.e("Connecting", "XMPPException: "+e.getMessage());
} catch (IllegalStateException e) {
Log.e("Connecting", "IllegalStateException: "+e.getMessage());
} catch (NullPointerException e) {
Log.e("Connecting", "NullPointerException: "+e.getMessage());
} catch (Exception e) {
Log.e("Connecting", "Exception: "+e.getMessage());
}
/* DECLARE classes AND objects BELOW SOMEWHERE*/
/* IncomingPacketListener class defined below */
public class IncomingPacketListener implements PacketListener {
@Override
public void processPacket(Packet packet) {
Log.i("IncomingPacketListener.processPacket: ", "--> "+packet.toXML());
// Todo do something else with the message
}
}
/* OutgoingPacketListener class defined below */
public class OutgoingPacketListener implements PacketInterceptor {
@Override
public void interceptPacket(Packet packet) {
Log.d("OutgoingPacketListener.interceptPacket", "<-- "+packet.toXML());
// Todo do something else with the message
}
}
/* ConnectionListener object is simply instance of ConnectionListener */
ConnectionListener connectionListener = new ConnectionListener() {
@Override
public void connectionClosed() {
Log.e("ConnectionListener.connectionClosed", "Connection closed.");
}
@Override
public void connectionClosedOnError(Exception e) {
Log.e("ConnectionListener.connectionClosedOnError", "Connection closed on error: "+e.getMessage());
}
@Override
public void reconnectingIn(int i) {
Log.i("ConnectionListener.reconnectingIn", "Reconnecting in " + i + " seconds.");
}
@Override
public void reconnectionSuccessful() {
Log.i("ConnectionListener.reconnectionSuccessful", "Reconnection successful.");
}
@Override
public void reconnectionFailed(Exception e) {
Log.e("ConnectionListener.reconnectionFailed", "Reconnection failed: "+e.getMessage());
}
};
Having a ConnectionConfiguration, as in http://codepad.co/s/6fb674, now we will be able to connect to the server.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.