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!
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
Ziga Klemencic
2,569 PointsCode Challenge 3/3 Getting Data from the web.
Now move the 'try' and two 'catch' blocks from the 'onCreate()' method to the 'doInBackground()' method. Leave the return statement as is, and then execute your custom async task from inside the 'onCreate()' method after 'setContentView()'.
Cant complete this challenge... i have been checking for any kind of error of wat i might be doing wrong but cant find anything. Please help me. Thanks for your time. Ziga :D here's the code:
package com.example;
import android.os.Bundle;
import android.view.View;
import java.io.IOException;
import java.net.MalformedURLException;
import android.util.Log;
import android.os.AsyncTask;
public class MainListActivity extends ListActivity {
public static final String URL = "http://www.teamtreehouse.com";
public static final String TAG = MainListActivity.class.getSimpleName();
private class CustomAsyncTask extends AsyncTask<Object, Void, String>{
@Override
protected String doInBackground(Object... arg0) {
return "";
try {
URL treehouseUrl = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) treehouseUrl.openConnection();
connection.connect();
int responseCode = connection.getResponseCode();
}
catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException caught!", e);
}
catch (IOException e) {
Log.e(TAG, "IOException caught!", e);
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
CustomAsyncTask customAsyncTask = new CustomAsyncTask();
customAsyncTask.execute();
}
}
4 Answers

Ben Rubin
Courses Plus Student 14,658 PointsMove the return
statement to the end of doInBackground
. Having the return
statement at the beginning of the method means that nothing below it will ever be executed, because the method will exit as soon as it hits the return
statement.
If you paste that code into Eclipse, you'll probably get a compiler error that says "unreachable code" or something like that. Moving the return
statement to the end of the method will fix that error.

Juan Gallardo
1,566 PointsThe return statement needs to be at the end. Here is the code that worked for me:
@Override
protected String doInBackground(Object... arg0) {
try {
URL treehouseUrl = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) treehouseUrl.openConnection();
connection.connect();
int responseCode = connection.getResponseCode();
}
catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException caught!", e);
}
catch (IOException e) {
Log.e(TAG, "IOException caught!", e);
}
return "";
}
}

Ziga Klemencic
2,569 PointsWOHO :D thx dude, this was really helpfoul. I get it now xD Thx for your help and time :D

Charles Welbeck
Courses Plus Student 9,745 PointsCompiler Error Please Help.
public static final String TAG = MainListActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
private class CustomAsyncTask extends AsyncTask<Object, Void, String>{
@Override
protected String doInBackground(Object... params){
try {
URL treehouseUrl = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) treehouseUrl.openConnection();
connection.connect();
int responseCode = connection.getResponseCode();
}
catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException caught!", e);
}
catch (IOException e) {
Log.e(TAG, "IOException caught!", e);
}
}
return "";
}
}
}