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

jinhwa yoo
jinhwa yoo
10,042 Points

helpme

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.