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
Juan Gallardo
1,568 PointsTask 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
Jaroslav Vankat
12,054 PointsYou need to get the response code from the connection variable. So you need to write
int httpResponse = connection.responseCode;
Jaroslav Vankat
12,054 PointsI'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();
Juan Gallardo
1,568 PointsJuan Gallardo
1,568 PointsThis did not work. I tried