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 Blog Reader Android App Getting Data from the Web Moving Work from the Main Thread to an AsyncTask

wei gan
wei gan
2,798 Points

I have a IOException error??

When I run the app, an IOException appear.

And I download the code from the treehouse website and import it to my Eclipse, the same error also appear, I don't know why. Is it because I am in China??

This is my code:

public class MainListActivity extends ListActivity {
    private String[] mBlogPostTitiles;
    public static final int NUMBER_OF_POST = 20;
    public static final String TAG = MainListActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_list);

        GetBlogPostTask getBlogPostTask = new GetBlogPostTask();
        getBlogPostTask.execute();
        //Toast.makeText(this, getString(R.string.no_item), Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class GetBlogPostTask extends AsyncTask<Object, Void, String> {
        int responseCode = -1;

        @Override
        protected String doInBackground(Object... arg0) {
            try {
            URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count="+ NUMBER_OF_POST);
            HttpURLConnection connection = (HttpURLConnection) 

            blogFeedUrl.openConnection();
            connection.connect();

            responseCode = connection.getResponseCode();
            Log.i(TAG, "respondCode is: " + responseCode);

        } catch (MalformedURLException e) {
            Log.e(TAG, "URL Errot caught", e);
        } catch (IOException e) {
            Log.e(TAG, "IO Errot caught", e);
        } catch (Exception e) {
            Log.e(TAG, "General Errot caught", e);
        }
            return "Code:" + responseCode;
        }
    }

}

1 Answer

wei gan
wei gan
2,798 Points

I got the answer. It is because I run the app on a real device, and that device didn't connect to the internet than I open the wifi, the problem disappear.