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

BlogReader Unknown Host error

I am at the "Moving Work from the Main Thread to an AsyncTask" in the Getting Data from the Web section of the blog reader app. Upon running the code I get a UnknownHostException stating "Unable to resolve host :"blog.teamtreehouse.com": No address associated with host name.

Running the blog link on my browser returns a status 'ok'. Just wondering if I was doing something wrong in the code.

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Can you paste in your exact code? It should have "http://" at the front, for example. Maybe we can troubleshoot it if we see how it's being used.

package com.ysr.blogreader;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

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);

        if(isNetworkAvaliable()){
            GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
            getBlogPostsTask.execute();         
        }else{
            Toast.makeText(this, "Network Unavailable!", Toast.LENGTH_LONG).show();
        }

        //Toast.makeText(this, getString(R.string.no_items), Toast.LENGTH_LONG).show();
    }


    private boolean isNetworkAvaliable() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo= manager.getActiveNetworkInfo();

        boolean isAvailable = false;
        if(networkInfo != null && networkInfo.isConnected()){
            isAvailable = true;
        }

        return isAvailable;

    }


    @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, "URLException caught: "+e, e);
                }
                catch (IOException e) {
                    Log.e(TAG, "IOException caught: "+e, e);
                }
                catch (Exception e) {
                    Log.e(TAG, "Exception caught: "+e, e);
                }

                return "Code: " + responseCode;
            }

        }
}

Here's the code from my MainListActivity.java which I am using currently. I got the same error even after importing the project files provided in that section into the eclipse and running it. I wonder if it's an error in my environment (I'm using the AndroidSDK 4,3 on windows).

Another thing I wanted to point out in the Log.e methods. Log.e(TAG, "Exception caught: ", e) only prints "Exception caught: " in LogCat. I concatenated the exception variable with the second argument to actually view the exception in LogCat. Dunno if you intended to just show it that way but I thought this would help debug the exceptions better so I pointed it out :)

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I wonder...could you fire up your emulator, open the browser, and type in the blog URL from your code?

http://blog.teamtreehouse.com/api/get_recent_summary/?count=20

If it works from the emulator then you would know it is a problem with the code. If the emulator cannot reach that URL, then it would indicate a problem with your setup (maybe a firewall or proxy or something else in network settings).

Finally figured out the problem It was all because I had switched from one wifi to another and had left the emulator running. I guess the emulator does not reconfigure its internet settings with that change. Restarting the emulator solved the issue.

Thanks! Looking forward to more projects soon

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Haha - that's a funny and frustrating error! Seems like it should totally just piggyback on your computer's connection.

If you're interested, check out GenyMotion for Android emulators. I just downloaded and installed it the other night but haven't actually tried it out yet. A lot of Android developers in my Twitter feed really seem to like it, though.

Ben Jakuben I've heard very good things about GenyMotion, but I haven't actually tried it out as well. The upside of the "slowness" of the emulator is definitely seeing potential bottlenecks that I wouldn't normally notice. Very helpful with the application I'm currently working on.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Haha - that's a great way to spin that as a positive. :smile: It's really useful to simulate slow network speeds, too!