105

我尝试遵循本教程: 从 Web 获取数据

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

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

我创建了一个私有类并使用 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);
}
}

任何帮助将不胜感激。

4

11 回答 11

148

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
于 2011-12-15T04:59:32.880 回答
120

Please, check if you have valid internet connection.

于 2011-04-29T08:31:43.940 回答
67

愿你获得许可

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

您可能忘记在移动设备或任何设备中打开互联网。

于 2013-07-25T11:29:15.720 回答
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.

于 2011-04-29T08:31:56.057 回答
11

如果您像我一样拥有 USB 互联网,则模拟器似乎不喜欢打开和关闭连接,因此您可能需要重新启动模拟器

于 2012-08-23T09:35:58.180 回答
6

由于您的主机无法通过 DNS 将这个错误转换为 IP 地址。

解决这个问题:

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

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
于 2015-08-28T01:23:25.873 回答
5

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

于 2014-04-27T15:42:28.650 回答
2

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

于 2017-04-07T15:06:22.997 回答
1

我在使用 Android 10 模拟器时遇到了同样的问题,我可以按照以下步骤解决该问题:

  1. 转到设置→网络和互联网→高级→私有DNS
  2. 选择私有 DNS 提供商主机名
  3. 输入:dns.google
  4. 点击保存

使用此设置,您的 URL 应该可以按预期工作。

于 2020-06-18T18:27:20.383 回答
0

重新启动模拟器在某些情况下有效。

于 2020-04-25T04:10:19.320 回答
0

如果您在 wifi 或 LAN 上间歇性地看到这种情况,但您的移动互联网连接似乎还不错,则很可能您的 ISP 的廉价网关路由器正在经历高流量负载。

您应该捕获这些错误并提醒用户关闭使用网络的任何其他应用程序。

通过在您的桌面上运行几个高清 youtube 视频来进行测试,或者只是去一个繁忙的星巴克。

于 2017-03-07T22:10:19.580 回答