java.net.UnknownHostException:服务器的主机名无效:本地

发布于 2024-11-17 08:45:06 字数 465 浏览 0 评论 0原文

我应该采取哪些步骤来解决该错误:

java.net.UnknownHostException: Invalid hostname for server: local

我在 Android 模拟器中添加了新的虚拟主机名,但结果返回到

 java.net.UnknownHostException virtualhostname at 
  java.net.InetAddress.lookUpHostByName(InetAddress.java:506)

When I type my virtualhost URL on my PC, it work on display.然而,当我在模拟器上运行并检查 Logcat 时,我无法读取或检查 HTTP 状态(如果是 200、202 或错误代码)。它只是返回到 UnknownHostException

What are the steps I should take to solve the error:

java.net.UnknownHostException: Invalid hostname for server: local

I added the new virtual host name at Android emulator but the result returns to

 java.net.UnknownHostException virtualhostname at 
  java.net.InetAddress.lookUpHostByName(InetAddress.java:506)

When I type my virtualhost URL on my PC, it works on display. Then again, when I ran on Emulator and check on Logcat, I couldn't be able to read or check the HTTP status if 200, 202, or an error code number. It simply returned to UnknownHostException

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

ま昔日黯然 2024-11-24 08:45:06

我的 mac 上也遇到了同样的问题。当我从终端 ping 我的 $HOSTNAME 并返回 ping: 无法解析 myHostName: 未知主机 时,我发现了问题。

解决方法:

  1. 在您的终端上echo $HOSTNAME

  2. 无论它显示什么主机名(比如myHostName),请尝试对其进行 ping 操作:ping myHostName。如果它返回ping:无法解析myHostName:未知主机,则在您的/etc/hosts文件中添加一个条目。

  3. 为此,编辑 /etc/hosts 文件并添加以下内容:

    127.0.0.1 myHostName

I was having the same issue on my mac. I found the issue when I pinged my $HOSTNAME from terminal and it returned ping: cannot resolve myHostName: Unknown host.

To resolve:

  1. Do echo $HOSTNAME on your terminal.

  2. Whatever hostname it shows (lets say myHostName), try to ping it : ping myHostName. If it returns ping: cannot resolve myHostName: Unknown host then add an entry into your /etc/hosts file.

  3. For that edit /etc/hosts file and add following:

    127.0.0.1 myHostName

小鸟爱天空丶 2024-11-24 08:45:06

异常的真正意思是没有名称为“local”的已知服务器。我的猜测是您正在尝试连接到本地计算机。尝试使用主机名 "localhost",或者 127.0.0.1::1(最后一个是 IPv6)。

来自 javadocs

抛出该异常表示IP地址
无法确定主机。

127.0.0.1::1"localhost" 应该始终是环回接口,所以如果这不起作用,我会真的很惊讶。

如果您的网络上确实有一个名为“本地”的服务器 - 检查您的 DNS 设置或将其添加到您的主机文件中。

What the exception is really saying is that there is no known server with the name "local". My guess is that you're trying to connect to your local computer. Try with the hostname "localhost" instead, or perhaps 127.0.0.1 or ::1 (the last one is IPv6).

From the javadocs:

Thrown to indicate that the IP address
of a host could not be determined.

127.0.0.1or ::1 or "localhost" should always be the loopback interface, so if that doesn't work I'd be really surprised.

If there really is a server called "local" on your network - examine your DNS settings or add it to your hosts file.

情定在深秋 2024-11-24 08:45:06

java.net.UnknownHostException:主机未解析:

抛出该异常表示无法确定主机的 IP 地址。

当您连接到有效的 wifi 但路由器无法接收互联网时,也会引发此异常。重现此问题非常容易:

  1. 连接到有效的 wifi
  2. 现在,在插入路由器时从路由器上拔下电缆,

您将观察到此错误!

你无法真正解决这个问题,你只能优雅地通知用户。 (类似于 - “无法建立连接”)

java.net.UnknownHostException: Host is unresolved:

Thrown to indicate that the IP address of a host could not be determined.

This exception is also raised when you are connected to a valid wifi but router does not receive the internet. Its very easy to reproduce this:

  1. Connect to a valid wifi
  2. Now remove the cable from the router while router is pluged-in

You will observe this error!!

You can't really solve this, You can only notify the user gracefully. (something like - "Unable to make a connection")

小…红帽 2024-11-24 08:45:06

这不是特定于这个问题的,但是当我在谷歌上搜索提到的 UnknownHostException 时出现了这个问题,并且在其他地方找不到修复程序,所以我想我应该在这里添加一个答案。

不断收到的异常是:

java.net.UnknownHostException:  google.com
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    ... 

无论我如何尝试连接到任何有效的主机,在终端中打印它也无济于事。一切都是对的。

解决方案

不调用 trim() 针对包含空格的主机字符串。在编写代理服务器时,通过使用分号作为 HOST 标头,使用 split(":") 从 HTTP 标头获取主机。这会留下空格,并导致 UnknownHostException 因为带有空格的主机不是有效的主机。在 String host 上执行 host = host.trim() 解决了这个不明确的问题。

This is not specific to the question, but this question showed up when I was Googling for the mentioned UnknownHostException, and the fix is not found anywhere else so I thought I'd add an answer here.

The exception that was continuously received was:

java.net.UnknownHostException:  google.com
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    ... 

No matter how I tried to connect to any valid host, printing it in the terminal would not help either. Everything was right.

The Solution

Not calling trim() for the host string which contained whitespace. In writing a proxy server the host was obtained from HTTP headers with the use of split(":") by semicolons for the HOST header. This left whitespace, and causes the UnknownHostException as a host with whitespace is not a valid host. Doing a host = host.trim() on the String host solved the ambiguous issue.

作死小能手 2024-11-24 08:45:06

您的主机名丢失。 JBoss 连接到服务器时使用此环境变量 ($HOSTNAME)。

[root@xyz ~]# echo $HOSTNAME
xyz

[root@xyz ~]# ping $HOSTNAME
ping: unknown host xyz

[root@xyz ~]# hostname -f
hostname: Unknown host

有很多原因可能导致这种情况。如果您发现新的原因,请评论。

为了在您的服务器上永久解决此问题之前进行破解,您可以在 /etc/hosts 文件的末尾添加一行:

127.0.0.1 xyz.xxx.xxx.edu xyz

Your hostname is missing. JBoss uses this environment variable ($HOSTNAME) when it connects to the server.

[root@xyz ~]# echo $HOSTNAME
xyz

[root@xyz ~]# ping $HOSTNAME
ping: unknown host xyz

[root@xyz ~]# hostname -f
hostname: Unknown host

There are dozens of things that can cause this. Please comment if you discover a new reason.

For a hack until you can permanently resolve this issue on your server, you can add a line to the end of your /etc/hosts file:

127.0.0.1 xyz.xxx.xxx.edu xyz
且行且努力 2024-11-24 08:45:06

发生这种情况的原因有多种

1) 检查您是否连接了 VPN,如果是,有时可能会出现此错误

“您的主机名,本地主机解析为环回地址:127.0.0.1;使用 10.xxx.1.193 代替(在接口 cscotun0 上) )"

2) 检查您的 $HOSTNAME

3) 尝试在命令行上 ping $HOSTNAME,如果不起作用,请调整系统设置以使您的本地主机响应 ping

This might happen due to various reasons

1) Check if you have VPN connected, you might get this error sometimes if yes

"Your hostname, localhost resolves to a loopback address: 127.0.0.1; using 10.xxx.1.193 instead (on interface cscotun0)"

2) Check your $HOSTNAME

3) try to ping $HOSTNAME on commandline and if it doesnt work, tweak the system settings to make your local host respond to pings

一身软味 2024-11-24 08:45:06

尝试以下操作:

String url = "http://www.google.com/search?q=java";
URL urlObj = (URL)new URL(url.trim());
HttpURLConnection httpConn = 
(HttpURLConnection)urlObj.openConnection();
httpConn.setRequestMethod("GET");
Integer rescode = httpConn.getResponseCode();
System.out.println(rescode);

Trim() URL

Try the following :

String url = "http://www.google.com/search?q=java";
URL urlObj = (URL)new URL(url.trim());
HttpURLConnection httpConn = 
(HttpURLConnection)urlObj.openConnection();
httpConn.setRequestMethod("GET");
Integer rescode = httpConn.getResponseCode();
System.out.println(rescode);

Trim() the URL

只是一片海 2024-11-24 08:45:06

请尝试将 SPARK_LOCAL_IP 环境变量设置为您想要连接的 IP 地址(可以是 localhost,即您自己的 IP 地址)。例如

$ export SPARK_LOCAL_IP=182.95.208.152

这样您就不需要更改现有的 Linux 设置。
为我工作,希望对你也有帮助。

Please try to set SPARK_LOCAL_IP environment variable to the ip address(can be localhost i.e. your own ip address) you want to connect. E.g.

$ export SPARK_LOCAL_IP=182.95.208.152

This way you will not be required to alter existing linux settings.
Worked for me, hope helps you too.

缪败 2024-11-24 08:45:06

尝试连接到您的本地计算机。请尝试使用主机名“localhost”,或者使用 ::/ - 最后一个是 ipv6

Trying to connect to your local computer.try with the hostname "localhost" instead or perhaps ::/ - the last one is ipv6

心头的小情儿 2024-11-24 08:45:06

如果您在这里是因为您的模拟器给出了该异常,请转到“工具”>“ Android 模拟器中的 AVD 管理器和冷启动模拟器。

If you are here because your emulator gives you that Exception, Go to Tools > AVD Manager in your android emulator and Cold boot your Emulator.

染年凉城似染瑾 2024-11-24 08:45:06

将您的手机连接不同服务提供商的不同 Wi-Fi 连接。
我不知道确切的问题,但我无法连接到特定服务提供商的服务器,但当我连接到其他服务提供商时它可以工作。所以尝试一下吧!

Connect your mobile with different wifi connection with different service provider.
I don't know the exact issue but i could not connect to server with a specific service provider but it work when i connected to other service provider. So try it!

芸娘子的小脾气 2024-11-24 08:45:06

当我抓取 xml 文件时,我的 Android 应用程序遇到了这个问题,我的链接格式无效,我使用完整的 url 重新格式化,然后它就工作了。

I had this issue in my android app when grabbing an xml file the format of my link was not valid, I reformatted with the full url and it worked.

秋叶绚丽 2024-11-24 08:45:06

我有同样的问题。

重新启动 docker 对我来说是解决方法。由于某种原因,它需要重新启动,我不知道为什么,但它有效。

I had the same issue.

Restart docker was the fix for me. For some reason it needed a restart, I don´t know why, but it worked.

┊风居住的梦幻卍 2024-11-24 08:45:06

如果您的 /etc/localhosts 文件具有如下条目:
添加主机名条目如下:

127.0.0.1 local host (add your hostname here)
::1 (add hostname here) (the last one is IPv6).

这应该可以解决问题。

If your /etc/localhosts file has entry as below:
Add hostname entry as below:

127.0.0.1 local host (add your hostname here)
::1 (add hostname here) (the last one is IPv6).

This should resolve the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文