net. unknownhostexception:无法解析主机[LOCAL]
I use the INTERNET
uses-permission in the manifest file.
我在manifest文件中使用互联网使用权限。
If I type URL url = new URL('http://www.example.com/page')
(remote server) it will work. But if I type URL url = new URL('http://example.local/page')
(local server) it will return the error
如果我输入URL = new URL('http://www.example.com/page')(远程服务器),它将会工作。但是如果我输入URL URL = new URL('http://example.local/page')(本地服务器),它将返回错误。
java.net.UnknownHostException: Unable to resolve host "example.local": No address associated with hostname
Now in %WINDIR%/System32/drivers/etc/hosts
I wrote this entry :
现在,在%WINDIR%/System32/drivers/etc/主机中,我写了这个条目:
10.101.0.179 example.local
I can't use new URL('http://10.101.0.179/page')
because example.local is a virtualHost in my apache configuration.
我不能使用新的URL('http://10.101.0.179/page'),因为示例。本地是apache配置中的虚拟主机。
Can't my AVD looks into the hosts file ? what should I do to tell android to fetch the ip 10.101.0.179
but still send a proper information to apache and tell it to serve the virtual host example.local
?
我的AVD不能查看主机文件吗?我应该怎么做才能告诉android获取ip 10.101.0.179,但仍然要向apache发送适当的信息并告诉它为虚拟主机示例服务。地方吗?
2 个解决方案
#1
1
If you try to request a web-page from your local HTTP server and the page is bounded to a virtualHost, so let say you need to reach http://example.local/mypage
and if as pointed by @CommonsWare you don't use mDNS to resolve inter-local domain-names, android will probably throw the same error message as it reads in the title of this question. Here's the solution I am using :
如果您试图从本地HTTP服务器请求一个web页面,并且该页面被绑定到一个虚拟主机,那么假设您需要访问http://example。如果如@CommonsWare所指出的,您不使用mDNS来解析本地/mypage,那么android很可能会在这个问题的标题中显示相同的错误消息。下面是我正在使用的解决方案:
let's say http://example.local/mypage
refers to the ip address 10.101.0.179
, Android won't be able to resolve example.local
, you need to write the raw url (of your actual local server)
假设http://example。本地/mypage是指ip地址10.101.0.179,Android无法解析的例子。本地,需要编写原始url(实际本地服务器的url)
try {
...
URL url = new URL("http://10.101.0.179/mypage");
HttpUrlConnection conn = (HttpUrlConnection) url.openConnection();
...
}
...
But now when Apache receives this request, unless serving your desired host, 10.101.0.179
won't be understood in case of a VirtualHost. To round-about this problem, just modify the Host
http-header property using setRequestProperty
:
但是现在,当Apache接收到这个请求时,除非服务于所需的主机,否则在使用虚拟主机时,10.101.0.179是无法理解的。要解决这个问题,只需使用setRequestProperty修改主机http-header属性:
...
conn.setRequestProperty("Host", "example.local");
...
And it should work.
它应该工作。