Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to resolve host "<insert URL here>" No address associated with hostname

I tried following this tutorial: Getting Data from the Web

I tried implementing it on Android 3.0, the latest platform for tablets, however, I get this error: "Unable to resolve host "www.anddev.org" No address associated with hostname."

You can checkout the URL that I used just to prove that the file exists. http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt

I created a private class and extended it with asynctask. Here is the code:

    private class Downloader extends AsyncTask<String,Void,String>{
    String myString = null;
    @Override
    protected String doInBackground(String... arg0) {
        try{
            URL myURL = new URL("http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt");
            URLConnection ucon = myURL.openConnection();
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while((current=bis.read())!=-1){
                baf.append((byte)current);
            }
            myString = new String (baf.toByteArray());
        }catch(Exception e){
            myString = e.getMessage();
        }
        return myString;
    }
@Override
protected void onPostExecute(String result){
    tv.setText(result);
}
}

Any help out there would be appreciated.

like image 454
황현정 Avatar asked Apr 29 '11 08:04

황현정


People also ask

What does no address associated with hostname mean?

The errors Failed host lookup: 'api.xyz.com' and OS Error: No address associated with hostname usually mean that your DNS lookup fails. As hinted by OS Error this is usually a system-level error and nothing specific to http or Dart.

How do I fix Sudo unable to resolve host?

Since the hostname is missing and your system is not able to figure out the hostname and thus it throws the error 'sudo: unable to resolve host'. To fix this error, edit the /etc/hosts file and set the hostname (newpc) with a loopback address (127.0. 0.1).

How do you handle an unknown host exception in Java?

A few tips to prevent the exception are: Double-check the hostname: Make sure there is no typo, and trim all whitespaces. Check the system's DNS settings: Make sure the DNS server is up and reachable, and if the hostname is new, wait for the DNS server to catch up.


2 Answers

My bet is that you forgot to give your app the permission to use the internet. Try adding this to your android manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
like image 174
leech Avatar answered Oct 14 '22 07:10

leech


Please, check if you have valid internet connection.

like image 120
Vladimir Ivanov Avatar answered Oct 14 '22 05:10

Vladimir Ivanov