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 Working with JSON Setting CurrentWeather from JSON

Seeking an explantion for this line of code

 response.body().string();

4 Answers

1)  if (isNetworkAvailable()) {
2)     OkHttpClient client = new OkHttpClient();

3)     Request request = new Request.Builder()
          .url(forecastURL)
          .build();

4)      Call call = client.newCall(request);
5)     call.enqueue(new Callback() {
        @Override
6)       public void onFailure(Call call, IOException e) {

        }

        @Override
7)        public void onResponse(Call call, Response response) throws IOException {
         try {
8)         String jsonData = response.body().string();
            Log.v(TAG, jsonData);
         } catch (JSONException e) {
            Log.e(TAG, "JSON Exception caught: ", e);
          }
        }
      });
    }
}

To answer your question, how we access the json file?

we do following steps...

1) create OkHttpClient object

2) create Request object

3) create Call object using Request object

up till line 4 we have just created the necessary objects required to perform request to server(in this case Sky Api)...

It is very important to understand that we have not made a request to the server up till line 4.

on line 5 we make a request by calling enqueue on Call object.... since it's going to take some time to get a response, we use callback object... which has two callback methods, onFailure() and onResponse().

onFailure() is called when the request could not be executed due to cancellation, a connectivity problem or timeout.

onResponse() is called when the HTTP response was successfully returned by the remote server.

inside onResponse() we get the response and we can check whether the response is successful ( in our case did we get our json file.? )

response is an object of Response class, which contains all the information related to the HTTP response. Using response object, we can find out whether the response was successful or not.

response.body() returns a non-null value if this response was passed. (response.body() actually returns another object ResponseBody)

Now in order to get the response as a string, we use string() method on response.body()

This is called Method chaining , in which calling method on object return another object, then call method on that object may return another object and so on...

In method chaining, Each method returns an object, allowing the calls to be chained together in a single statement without introducing a new variables.

for more info https://square.github.io/okhttp/3.x/okhttp/okhttp3/Response.html https://en.wikipedia.org/wiki/Method_chaining

thanks for the explanation, i have few other question, how exactly we access the json file via the sample app call?, we do it by first creating an okhttp object, then we use the Request.builder to request access to the dark sky sample api call, then we use a Call object and passing in the request in order to do what? then we use the call.eneque method and after that in the on response method we call response.body().string() , this line of code returns the json data if we successfully made a connection?

Marie Franklin

Marie Franklin Thank you very much for taking ur time and explain step by step the code ! :D so in the end, after we make a request on the server we use the Response object >> response.body().string() which return the json data if the http response was successfully accpeted by the remote server?

Yes!!!