无法解析主机“”没有与主机名关联的地址

时间:2022-09-03 21:25:38

I tried following this tutorial: Getting Data from the Web

我尝试过本教程:从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."

我尝试在平板电脑的最新平台Android 3.0上实现它,但是,我收到此错误:“无法解析主机”www.anddev.org“没有与主机名关联的地址。”

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

您可以签出我用来证明该文件存在的URL。 http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt

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

我创建了一个私有类,并使用asynctask扩展它。这是代码:

    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.

任何帮助将不胜感激。

9 个解决方案

#1


140  

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

我敢打赌,你忘了给你的应用程序使用互联网的许可。尝试将此添加到您的Android清单:

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

#2


103  

Please, check if you have valid internet connection.

请检查您是否有有效的互联网连接。

#3


56  

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.

#4


10  

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.

如果您使用的是OS / X,则模拟器使用“第一个”接口,即使您使用的是无线(en1),因为没有电缆的en0仍标记为up。您可以向下发出ifconfig en0并重新启动模拟器。我想我已经阅读过关于Windows的类似行为。

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

如果您使用的是Wifi / 3G,请致电您的网络提供商以获取正确的DNS设置。

#5


9  

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

如果你有像我这样的USB网络,模拟器似乎不喜欢打开和关闭连接,所以你可能需要重新启动模拟器

#6


5  

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

由于您的主机导致的此错误无法通过DNS转换为IP地址。

Solve of this problem :

解决这个问题:

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

1-确保连接到互联网(检查网络质量)。

2- Make sure you take proper permission to access network

2-确保您获得访问网络的适当权限

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

#7


4  

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.

我得到了同样的错误,问题是我在VPN上,我没有意识到这一点。断开VPN并重新连接wifi解决后。

#8


2  

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

此外,请确保您的设备未处于飞行模式和/或已启用数据使用。

#9


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.

如果您在wifi或LAN上间歇性地看到这一点,但您的移动互联网连接似乎没问题,那么您的ISP便宜的网关路由器很可能正在经历高流量负载。

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.

通过在桌面上运行几个HD youtube视频进行测试以重现,或者只是去繁忙的星巴克。

#1


140  

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

我敢打赌,你忘了给你的应用程序使用互联网的许可。尝试将此添加到您的Android清单:

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

#2


103  

Please, check if you have valid internet connection.

请检查您是否有有效的互联网连接。

#3


56  

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.

#4


10  

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.

如果您使用的是OS / X,则模拟器使用“第一个”接口,即使您使用的是无线(en1),因为没有电缆的en0仍标记为up。您可以向下发出ifconfig en0并重新启动模拟器。我想我已经阅读过关于Windows的类似行为。

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

如果您使用的是Wifi / 3G,请致电您的网络提供商以获取正确的DNS设置。

#5


9  

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

如果你有像我这样的USB网络,模拟器似乎不喜欢打开和关闭连接,所以你可能需要重新启动模拟器

#6


5  

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

由于您的主机导致的此错误无法通过DNS转换为IP地址。

Solve of this problem :

解决这个问题:

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

1-确保连接到互联网(检查网络质量)。

2- Make sure you take proper permission to access network

2-确保您获得访问网络的适当权限

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

#7


4  

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.

我得到了同样的错误,问题是我在VPN上,我没有意识到这一点。断开VPN并重新连接wifi解决后。

#8


2  

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

此外,请确保您的设备未处于飞行模式和/或已启用数据使用。

#9


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.

如果您在wifi或LAN上间歇性地看到这一点,但您的移动互联网连接似乎没问题,那么您的ISP便宜的网关路由器很可能正在经历高流量负载。

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.

通过在桌面上运行几个HD youtube视频进行测试以重现,或者只是去繁忙的星巴克。