Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Getting different exception from BlogReader Async Task video.

I'm using Android Studio.

When following the video, first off I didn't seem to get prompted for any values to go in the angle brackets after AsyncTask, this didn't throw up an error either. I added them as per the video and still did not get an error. According to this point in the video I should be getting a security exception.

After moving the try/catch code in and updating the return value I am still getting the following:

```08-24 17:22:03.299 1189-1189/net.thefoldgaming.blogreader E/MainListActivity﹕ Exception caught: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145) at java.net.InetAddress.lookupHostByName(InetAddress.java:385) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) at java.net.InetAddress.getAllByName(InetAddress.java:214) at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28) at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216) at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292) at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89) at net.thefoldgaming.blogreader.MainListActivity.onCreate(MainListActivity.java:31) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

Here is my code for the GetBlogPostsTask method:

```java
    private class GetBlogPostsTask 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_POSTS);
                HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
                connection.connect();

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

Also, when performing the doInBackground override the method had a variable of (Object[] object) instead of Object...arg0.

When first setting up the project I did not have the option for the version of Android that is used for this project so I used the lowest version given by the copy of Android Studio supplied with the first project.

1 Answer

I've fixed this myself. Upon looking at the code again, Android Studio said that the semicolon at the end of the catch was not needed. I removed it and now it's running and reporting the security exception I was expecting!