Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

smack api for android, Connection.isConnected().

I am testing my chat app on android.

one of the test scenarios is when I turn off the wifi on my server, The client thinks it is still connected, because isConnected() method returns true.

So I am unable to validate the connection in this particular scenario. Any thoughts on this problem? Thank you.

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

By "turnn off wifi on my server", do you mean you are disconnecting the server that you are using as the back-end? In that case the phone will still be connected to the Internet, but when it attempts to connect to your service it should get an error. You'll need to add error handling when that attempt to connect is made.

Yes, I am the disconnecting the server, my call for reconnect method is done only when isConnected() returns false, but it returns true in this case(it lies). Thank you Ben.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I am not familiar with the Smack API, but I took a quick look at their documentation. Perhaps you could try ConnectionConfiguration#setReconnectionAllowed(boolean). From the docs:

By default Smack will try to reconnect the connection in case it was abruptly disconnected. Use ConnectionConfiguration#setReconnectionAllowed(boolean) to turn on/off this feature. The reconnection manager will try to immediately reconnect to the server and increase the delay between attempts as successive reconnections keep failing.

http://www.igniterealtime.org/builds/smack/docs/latest/documentation/connections.html