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

Task 2 of 3 code challenge. Establish a connection to the URL using the appropriate method of the 'connection' variable

Pretty sure that this is way off

try {
    URL treehouseUrl = new URL(URL);
    HttpURLConnection connection  = (HttpURLConnection) treehouseUrl.openConnection();
    connection.connect();
    int httpResponse = getResponseCode();
}

2 Answers

You need to get the response code from the connection variable. So you need to write

int httpResponse = connection.responseCode;

This did not work. I tried

try {
        URL treehouseUrl = new URL(URL);
        HttpURLConnection connection  = (HttpURLConnection) treehouseUrl.openConnection();
        connection.connect();
        int httpResponse = connection.responseCode;
}

I'm really sorry, my fault, it's not

int httpResponse = connection.responseCode;

but

int httpResponse = connection.getResponseCode();

So the code should look like that (this works for me):

            URL treehouseUrl = new URL(URL);
            HttpURLConnection connection = (HttpURLConnection) treehouseUrl.openConnection();
            connection.connect();
            int httpResponse = connection.getResponseCode();