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

Not sure what I am doing wrong here but I am getting I/O exception below is the code

package com.example.blogreader;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainListActivity extends ListActivity {
    protected String[] mHospitals;
    public static final int NUMBER_OF_HOSPITALS = 20;
    public static final String TAG = MainListActivity.class.getSimpleName();

    protected void onCreate(Bundle savedInstanceState) {

        GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();


    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;

    private class GetBlogPostsTask extends AsyncTask<Object, Void, String> {

        protected String doInBackground(Object... arg0) {
            int reponseCode = -1;

            try {

                URL hospitalFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_HOSPITALS);
                HttpURLConnection connection = (HttpURLConnection) hospitalFeedUrl.openConnection();
                reponseCode = connection.getResponseCode();
                Log.i(TAG, "Code " + reponseCode);
            catch (MalformedURLException e) {
                Log.e(TAG, "exception caught: ", e);    
            catch (IOException e) {
                Log.e(TAG, "exception IO caught: ", e); 
            catch(Exception e){
                Log.e(TAG, "General exception", e);

            return "Code "+ reponseCode;    



2 Answers

I think thats the buffer issue. I was able to work around it like this

InputStream is = response.getEntity().getContent();
                        BufferedInputStream bis = new BufferedInputStream(is);
                        ByteArrayBuffer baf = new ByteArrayBuffer(20);
                        int current = 0;
                        while ((current = bis.read()) != -1) {
                            baf.append((byte) current);
                        /* Convert the Bytes read to a String. */
                        responseData = new String(baf.toByteArray());


However I wished there was a better way described in tutorial.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Is this still an issue? Your code looks okay, so it could have been an intermittent error with the blog. If you are still getting the error, copy and paste all the detail you can about the IO exception.