java.net.UnknownHostException:服务器的主机名无效:本地
我应该采取哪些步骤来解决该错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我的 mac 上也遇到了同样的问题。当我从终端 ping 我的
$HOSTNAME
并返回ping: 无法解析 myHostName: 未知主机
时,我发现了问题。解决方法:
在您的终端上
echo $HOSTNAME
。无论它显示什么主机名(比如
myHostName
),请尝试对其进行 ping 操作:ping myHostName
。如果它返回ping:无法解析myHostName:未知主机
,则在您的/etc/hosts
文件中添加一个条目。为此,编辑
/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 returnedping: cannot resolve myHostName: Unknown host
.To resolve:
Do
echo $HOSTNAME
on your terminal.Whatever hostname it shows (lets say
myHostName
), try to ping it :ping myHostName
. If it returnsping: cannot resolve myHostName: Unknown host
then add an entry into your/etc/hosts
file.For that edit
/etc/hosts
file and add following:127.0.0.1 myHostName
异常的真正意思是没有名称为“local”的已知服务器。我的猜测是您正在尝试连接到本地计算机。尝试使用主机名
"localhost"
,或者127.0.0.1
或::1
(最后一个是 IPv6)。来自 javadocs:
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 perhaps127.0.0.1
or::1
(the last one is IPv6).From the javadocs:
127.0.0.1
or::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.
抛出该异常表示无法确定主机的 IP 地址。
当您连接到有效的 wifi 但路由器无法接收互联网时,也会引发此异常。重现此问题非常容易:
您将观察到此错误!
你无法真正解决这个问题,你只能优雅地通知用户。 (类似于 - “无法建立连接”)
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:
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")
这不是特定于这个问题的,但是当我在谷歌上搜索提到的 UnknownHostException 时出现了这个问题,并且在其他地方找不到修复程序,所以我想我应该在这里添加一个答案。
不断收到的异常是:
无论我如何尝试连接到任何有效的主机,在终端中打印它也无济于事。一切都是对的。
解决方案
不调用
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:
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 ofsplit(":")
by semicolons for theHOST
header. This left whitespace, and causes theUnknownHostException
as a host with whitespace is not a valid host. Doing ahost = host.trim()
on theString host
solved the ambiguous issue.您的主机名丢失。 JBoss 连接到服务器时使用此环境变量 ($HOSTNAME)。
有很多原因可能导致这种情况。如果您发现新的原因,请评论。
为了在您的服务器上永久解决此问题之前进行破解,您可以在 /etc/hosts 文件的末尾添加一行:
Your hostname is missing. JBoss uses this environment variable ($HOSTNAME) when it connects to the server.
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:
发生这种情况的原因有多种
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
尝试以下操作:
Trim() URL
Try the following :
Trim() the URL
请尝试将 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.
尝试连接到您的本地计算机。请尝试使用主机名“localhost”,或者使用 ::/ - 最后一个是 ipv6
Trying to connect to your local computer.try with the hostname "localhost" instead or perhaps ::/ - the last one is ipv6
如果您在这里是因为您的模拟器给出了该异常,请转到“工具”>“ 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.
将您的手机连接至不同服务提供商的不同 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!
当我抓取 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.
我有同样的问题。
重新启动 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.
如果您的 /etc/localhosts 文件具有如下条目:
添加主机名条目如下:
这应该可以解决问题。
If your /etc/localhosts file has entry as below:
Add hostname entry as below:
This should resolve the issue.