Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Weather App Concurrency and Error Handling Configuring the AlertDialog

I am confused with the onFailure method and onResponse Method.

I understand we have both of these methods although why are we using a try and catch block within the onReponse Method. Doesn't onResponse only get called if the message was successful meaning we should use the alertUserAboutError only in the onFailure method? I am sorry if this makes no sense I am just a little confused with these lines of code.

Other than that you have been an amazing teacher and I couldn't be more happier learning through tree house!

1 Answer

Ben Deitch
Ben Deitch
Treehouse Teacher

Hi Aleksei! Whenever you make an HTTP call, if the call makes it to the server (i.e. you and the server are both connected to the internet), you'll get one of these response codes. However, only some of these codes indicate a successful response. Others represent something else (e.g. 404 means not found). So in order for a response to be successful, it needs to return a status code in the 200 range. And if we look at the documentation for 'isSuccessful' in OkHttp, that's exactly what we see.