无法解析主机“<在此处插入 url>”没有与主机名关联的地址在此处插入>
我尝试按照本教程进行操作: 获取数据来自网络
我尝试在最新的平板电脑平台 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);
}
}
如有任何帮助,我们将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我敢打赌,您忘记授予您的应用程序使用互联网的权限。尝试将其添加到您的 Android 清单中:
My bet is that you forgot to give your app the permission to use the internet. Try adding this to your android manifest:
请检查您是否有有效的互联网连接。
Please, check if you have valid internet connection.
也许您已获得许可
但是
您可能忘记在移动设备或任何设备中打开互联网。
May you have taken permission
BUT
You may have forgot to TURN ON Internet in Mobile or Whatever Device.
您可以从内置浏览器中访问该网址吗?
如果不是,则说明您的网络设置不正确。如果您在模拟器中,您可以查看网络部分< /a> 文档。
如果您使用的是 OS/X,模拟器将使用“第一个”接口 en0,即使您使用的是无线 (en1),因为没有电缆的 en0 仍标记为已启动。您可以发出
ifconfig en0 down
并重新启动模拟器。我想我已经读到过 Windows 上的类似行为。如果您使用 Wifi/3G,请致电您的网络提供商以获取正确的 DNS 设置。
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.
如果您像我一样拥有 USB 互联网,模拟器似乎不喜欢打开和关闭连接,因此您可能需要重新启动模拟器
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
此错误是因为您的主机无法通过 DNS 转换为 IP 地址。
1- 确保您连接到互联网(检查网络质量)。
2-确保您获得访问网络的适当权限
This error because of you host cann't be translate to IP addresses via DNS.
1- Make sure you connect to the internet (check quality of network).
2- Make sure you take proper permission to access network
我遇到了同样的错误,问题是我使用的是 VPN,但我没有意识到这一点。断开VPN并重新连接wifi后解决。
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.
另外,请确保您的设备未处于飞行模式和/或已启用数据使用。
Also, make sure that your device isn't on airplane mode and/or that data usage is enabled.
我在使用 Android 10 模拟器时遇到了同样的问题,我可以按照以下步骤解决该问题:
通过此设置,您的 URL 应该按预期工作。
I had the same issue with the Android 10 emulator and I was able to solve the problem by following the steps below:
With this setup, you URL should work as expected.
如果您在 wifi 或 LAN 上间歇性地看到此情况,但您的移动互联网连接似乎正常,则很可能您的 ISP 的廉价网关路由器正在经历高流量负载。
您应该捕获这些错误并向用户显示提醒以关闭使用网络的任何其他应用程序。
通过在桌面上运行几个高清 YouTube 视频进行测试来重现,或者只是去繁忙的星巴克。
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.
在某些情况下重新启动模拟器是有效的。
Restarting the emulator works in some scenario.