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

Kate Hoferkamp
Kate Hoferkamp
5,205 Points

Exception error when running BlogReader App after creation of AsyncTask

After completing the video, when I run the BlogReader app, it catches an exception and does not even get far enough in the code to display a "responseCode". Here is my code and logCat, let me know what I'm missing please! Hopefully it's something simple =]

MainListActivity Class

public class MainListActivity extends ListActivity {

    protected String[] mBlogPostTitles;
    public static final int NUMBER_OF_POSTS = 20;
    public static final String TAG = MainListActivity.class.getSimpleName();

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

        GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
        getBlogPostsTask.execute();
    }

    @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;
    }

    private class GetBlogPostsTask extends AsyncTask<Object, Void, String> {
        @Override
        protected String doInBackground(Object... arg0) {

            int responseCode = -1;

            try {
                URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
                HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
                connection.connect();

                responseCode = connection.getResponseCode();
                Log.i(TAG, "Code: " + responseCode);
            }
            catch (MalformedURLException e){
                Log.e(TAG, "Exception Caught", e);
            }
            catch (IOException e){
                Log.e(TAG, "Exception Caught", e);
            }
            catch (Exception e){
                Log.e(TAG, "Exception Caught", e);
            }

            return "Code: " + responseCode;
        }
    }
}

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.peter.blogreader" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainListActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Logcat

09-05 15:28:08.981 1381-1381/com.example.peter.blogreader D/dalvikvm﹕ GC_FOR_ALLOC freed 60K, 6% free 2987K/3156K, paused 8ms, total 25ms

09-05 15:28:08.985 1381-1381/com.example.peter.blogreader I/dalvikvm-heap﹕ Grow heap (frag case) to 4.094MB for 1127532-byte allocation

09-05 15:28:09.061 1381-1390/com.example.peter.blogreader D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 5% free 4087K/4260K, paused 77ms, total 77ms

09-05 15:28:09.285 1381-1394/com.example.peter.blogreader E/MainListActivity﹕ Exception Caught

09-05 15:28:09.453 1381-1381/com.example.peter.blogreader D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so

09-05 15:28:09.453 1381-1381/com.example.peter.blogreader D/﹕ HostConnection::get() New Host Connection established 0xb7a65800, tid 1381

09-05 15:28:09.481 1381-1381/com.example.peter.blogreader D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so

09-05 15:28:09.481 1381-1381/com.example.peter.blogreader D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so

09-05 15:28:09.709 1381-1381/com.example.peter.blogreader W/EGL_genymotion﹕ eglSurfaceAttrib not implemented

09-05 15:28:09.709 1381-1381/com.example.peter.blogreader E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache

09-05 15:28:09.709 1381-1381/com.example.peter.blogreader E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384

09-05 15:28:09.725 1381-1381/com.example.peter.blogreader E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()

09-05 15:28:09.725 1381-1381/com.example.peter.blogreader E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384

09-05 15:28:09.725 1381-1381/com.example.peter.blogreader D/OpenGLRenderer﹕ Enabling debug mode 0

09-05 15:28:09.737 1381-1381/com.example.peter.blogreader W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=3794, eventTime=731663, downTime=533337, deviceId=1, source=0x301 }

09-05 15:28:09.737 1381-1381/com.example.peter.blogreader W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=3795, eventTime=732453, downTime=533337, deviceId=1, source=0x301 }

09-05 15:28:09.741 1381-1381/com.example.peter.blogreader W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=3796, eventTime=732750, downTime=533337, deviceId=1, source=0x301 }

09-05 15:28:10.165 1381-1381/com.example.peter.blogreader W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=3797, eventTime=732801, downTime=533337, deviceId=1, source=0x301 }

09-05 15:28:10.165 1381-1381/com.example.peter.blogreader W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=3798, eventTime=732852, downTime=533337, deviceId=1, source=0x301 }

09-05 15:28:10.169 1381-1381/com.example.peter.blogreader W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=3799, eventTime=733176, downTime=533337, deviceId=1, source=0x301 }

Kate Hoferkamp
Kate Hoferkamp
5,205 Points

Okay, I think I figured it out, Genymotion does not have network access so when I did the next video, it showed the toast that it had no network connection.

When I ran the app using my phone as the device through USB, I got the Code to display in logCat.