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) Networking Making an HTTP GET Request with OkHttp

jinhwa yoo
jinhwa yoo
10,042 Points


OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(forecastUrl).build(); Call call = client.newCall(request); try { Response reponse = call.execute(); if(reponse.isSuccessful()){ Log.v(TAG, reponse.body().string()); } } catch (IOException e) { Log.e(TAG, "Exception caught: ", e); }

-----------------------------------ohmygot----------------------------------------- helpmetounderstandthiscodingmean..Iamstucked.....

1 Answer

Andre' Jones
Andre' Jones
26,671 Points

This code is using OkHttp to make a request to your API. Its like putting "google.com" into your address bar. When you invoke call.execute() this is like typing enter or clicking the go button in your browser, It performs the actual call and eventually returns some response. If your response is a success you are logging a part of the info to the console, otherwise your doing nothing. If you throw an Exception anywhere in the try block you are catching it in the Catch block so it doesnt stop the app and logging the exception out to the console.