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 Making Our Code Asynchronous

new Callback() wants to be implemented differently

call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

            try {

                if (response.isSuccessful()){
                    Log.v(TAG,response.body().string());
                }
            } catch (IOException e) {
                Log.e(TAG,"exception caught: ",e);
            }

        }

    });

When I use auto complete to complete the code it wants the OnFailure function to take in a call parameter instead of a request, and the onResponse function also needs a Call. When I run the code, it didn't come out as seen on the video. Could this be because im using OkHttp3?

Bart Kluczynski
Bart Kluczynski
15,326 Points

Hi there,

I have exactly the same problem here.

Cheers,

Dillon Shaw
Dillon Shaw
6,400 Points

What happens when you run the code? I had to take the try...catch block out completely because at the catch block it complained that onResponse does not throw an IOException.. I replaced it to do this:

`  @Override
            public void onResponse(Call call, Response response) throws IOException {
              if(response.isSuccessful())
                  Log.i(TAG, response.body().toString());
              else
                  Log.e(TAG, "Exception caught", new IOException());
            }

So now at least the app will run, but in the log instead of providing the complete forecast body, it simply states: MainActivity: okhttp3.internal.http.RealResponseBody@52785108

I did a debug and it gave a response of 200, but not sure why it won't post the complete response body.

5 Answers

I have exactly the same code as you guys using OkHTTP3, and it works as described in the video. Did you try looking at the response body in the debugger?

Justin Roberts
Justin Roberts
14,583 Points

I was having a similar problem. It's kind of misleading because in the try statement it's trying an if statement, and if the if statement comes up false it's not that the try statement isn't working and thus throwing the catch statement, it's just the body in the if statement isn't executing. You can try adding an else statement and adding a log code to see if that is the case.

For me I had the forecast URL entered incorrectly. I would suggest double checking it to make sure it is accurate. I changed that and it works fine for me now. The additional parameters for the function (the Call call) doesn't seem to make a difference.

Marko Vulic
Marko Vulic
5,079 Points

Anyone had a solution for this problem?

William Bruntrager
William Bruntrager
11,473 Points

When you say that the response doesn't come out like in the video, you probably need to be more specific so that people can help diagnose and solve your problem.

At any rate, the following callback worked for me:

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Log.v(TAG, response.body().string());
                }
                else {
                    Log.i(TAG, "There was an error");
                }
            }
        });

If it isn't working, a good thing to do is double-check the forecastUrl you are using to be sure it is returning JSON. One helpful method is to include

Log.v(TAG, forecastUrl);

So you can read the URL from the log. Test the URL in a web browser to make sure that you are getting a JSON response.

Boban Talevski
Boban Talevski
24,793 Points

I guess you all figured it out by know, but just in case. As noted in another answer, you should be using

Log.v(TAG, response.body().string());

instead of

Log.v(TAG, response.body().toString());

And thanks for making me see why :).