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 Build a Weather App (2015) Concurrency and Error Handling What To Do When the Network is Down

pawkan kenluecha
pawkan kenluecha
26,303 Points

Is it obligated to check a network present ?

I'm just curious about this. Is it suffice to check only connectivity ? Because if no connectivity , it means no network presents anyway.

2 Answers

Matthew Francis
Matthew Francis
6,967 Points

Hi pawkan! I have the same exact thoughts as you! Since this was posted 4 months ago, mind if I ask if you have found the answer yet?

pawkan kenluecha
pawkan kenluecha
26,303 Points

To be honest, currently, I'm working on another subject right now. so the answer is no. I haven't pursued this topic anymore. Haha, hopefully, the staff would say something about it soon.

Thomas Williams
Thomas Williams
9,447 Points

I did wonder this at first and do not 100% know the answer but my best guess is that we are first asking networkInfo whether or not it has the answers to questions we could ask it and then if it does have the answers we ask it if we are connected to the internet. However, if we asked networkInfo are we connected to the internet but networkInfo didn't have the answers then it would probably return with an error and maybe cause the app to crash. So basically if networkInfo has no information (networkInfo = null) for whatever reason, then it won't know whether there is a connection or not and can't return an answer. By using the code 'if (networkInfo != null && networkInfo.isConnected())' we are first checking whether networkInfo holds the answers, if not then we move on with our code (out of the 'if' statement) without asking the next question which would return an error. I hope that makes some sense.