104

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.

Timmy
  • 4,098
  • 2
  • 14
  • 34
황현정
  • 3,451
  • 6
  • 28
  • 35

11 Answers11

148

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" />
leech
  • 8,293
  • 7
  • 62
  • 78
  • always see logcat to get the kind of error then your time consumption will be saved ...... when ever you forget to add permission .. logcat show the error ...... or either generate the crash report ........... – Vipin Sahu Aug 23 '12 at 10:06
  • 14
    well i also have this problem. I have permissions set up correctly. URLConnection is working fine most of the time. But sometimes, i get this error several times in a row. It is especially hard to debug, since it only comes up from time to time... – hendrix Nov 22 '12 at 12:14
  • You also have my permission to feel great for helping community – Sebastien FERRAND Jan 27 '13 at 13:08
  • I have added this permission then also i got same Error, Is there any Problem in Emulator – Pratik Butani Jul 23 '13 at 12:48
  • You dint need to access the network state to just give your app permission to connect to the internet :) – Bijay Koirala Jul 14 '14 at 07:38
  • 7
    I have encountered this error because I had turned off mobile data previously and forgot to turn it back on. It worked fine in the office, but as soon as I was off familiar wifi, I was getting the error. Sometimes it's the stupid things :| – Karmic Coder Aug 31 '14 at 21:34
  • What if I want to work on 3G and not wifi? I'm still getting this error in this case – Alaa M. Nov 19 '15 at 22:33
  • Thanks lad, access network state permission removed me from the horror – George Otieno May 22 '16 at 19:29
  • I lost my valuable 8 hours just trying to fix this error. A life savor. – Neyomal Aug 16 '17 at 01:12
  • @KarmicCoder's answer led me to the right answer - the device was not on a network that had access to the URL endpoint I was trying to connect to! – beyondtheteal Jul 18 '19 at 13:35
120

Please, check if you have valid internet connection.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • hmm... :D I checked it, and it seems like you're right. I really don't know why, but my internet connection is okay, however, the browser inside the emulator can't access anything. That's odd.. The emulator browser was working fine a few minutes ago. – 황현정 Apr 29 '11 at 08:40
  • 1
    I feel like the biggest idiot. I was developing at home, instead of the office and I forgot to setup WiFi. Sometimes it takes things like these to make you remember you're an idiot sometimes. So, have a wholehearted thank you for making me feel like an idiot! :) – RileyE May 22 '13 at 02:02
  • 2
    Same for me! somehow Wifi and airplane mode can be selected so i only had to disable the airplane mode (i wasnt really connected to the internet-palmface) – d1jhoni1b Mar 18 '14 at 22:36
  • 1
    Lol dunno why I expected my S3 to login without wireless turned on. – Declan McKenna Jun 24 '14 at 09:08
  • Damit. I turned off the internet connection to the simulator, yesterday and forgot to turn it on this morning, Thanks! – Robert J. Clegg Aug 02 '17 at 06:42
  • I have given both permission, but still, our user gets the above error. it reflect on firebase Crashlytics. please provide some other solution. – Pawan Singh Nov 14 '22 at 08:01
67

May you have taken permission

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

BUT

You may have forgot to TURN ON Internet in Mobile or Whatever Device.

Community
  • 1
  • 1
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
12

Are you able to reach that url from within the built-in browser?

If not, it means that your network setup is not correct. If you are in the emulator, you may have a look at the networking section of the docs.

If you are on OS/X, the emulator is using "the first" interface, en0 even if you are on wireless (en1), as en0 without a cable is still marked as up. You can issue ifconfig en0 down and restart the emulator. I think I have read about similar behavior on Windows.

If you are on Wifi/3G, call your network provider for the correct DNS settings.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • hmm... :D I checked it, and it seems like you're right. I really don't know why, but my internet connection is okay, however, the browser inside the emulator can't access anything. That's odd.. The emulator's browser was working fine a couple of minutes ago. – 황현정 Apr 29 '11 at 08:40
  • Thanks! In my case an intranet was set as the URL causing the error, using an extranet obviously solved the issue – CGR Nov 14 '17 at 23:25
11

if you have USB internet like me the emulator seems to dislike connection being turned on and off so you may need to restart the emulator

Ian
  • 111
  • 1
  • 2
6

This error because of you host cann't be translate to IP addresses via DNS.

Solve of this problem :

1- Make sure you connect to the internet (check quality of network).

2- Make sure you take proper permission to access network

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
5

I got the same error and for the issue was that I was on VPN and I didn't realize that. After disconnecting the VPN and reconnecting the wifi resolved it.

adubey
  • 492
  • 5
  • 8
2

Also, make sure that your device isn't on airplane mode and/or that data usage is enabled.

Dave Justen
  • 151
  • 5
1

I had the same issue with the Android 10 emulator and I was able to solve the problem by following the steps below:

  1. Go to Settings → Network & internet → Advanced → Private DNS
  2. Select Private DNS provider hostname
  3. Enter: dns.google
  4. Click Save

With this setup, you URL should work as expected.

Kylo-Rey
  • 1,266
  • 10
  • 12
  • This seems to be working for me so far. What exactly is this fix doing? Thanks. – Peter G. Williams Jan 31 '22 at 21:53
  • 1
    @PeterG.Williams, it appears the default Android DNS on the emulator cannot do the proper domain translation and so, with this setup, we explicitly ask to use Google's DNS services. – Kylo-Rey Feb 10 '22 at 18:58
0

If you see this intermittently on wifi or LAN, but your mobile internet connection seems ok, it is most likely your ISP's cheap gateway router is experiencing high traffic load.

You should trap these errors and display a reminder to the user to close any other apps using the network.

Test by running a couple of HD youtube videos on your desktop to reproduce, or just go to a busy Starbucks.

Dominic Cerisano
  • 3,522
  • 1
  • 31
  • 44
0

Restarting the emulator works in some scenario.

Ramdhas
  • 1,765
  • 1
  • 18
  • 26