情况是这样的。我的应用程序运行良好,并且能够与 URL 建立连接。但是在让应用程序运行几个小时后,Facebook 请求突然给我以下错误。
09-26 10:01:25.175: W/System.err(252): java.net.UnknownHostException: Host is unresolved: xyz.com:80
09-26 10:01:25.175: W/System.err(252): at java.net.Socket.connect(Socket.java:1037)
09-26 10:01:25.175: W/System.err(252): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
09-26 10:01:25.175: W/System.err(252): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:145)
09-26 10:01:25.175: W/System.err(252): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:67)
09-26 10:01:25.175: W/System.err(252): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:821)
09-26 10:01:25.175: W/System.err(252): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:807)
09-26 10:01:25.175: W/System.err(252): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1051)
09-26 10:01:25.175: W/System.err(252): at java.net.URL.openStream(URL.java:653)
此错误在应用程序和模拟器上都会发生。当我注销我的应用程序并重新连接到 Facebook 时,连接再次工作。
我应该注意:当我与自己的服务器建立连接时,没有出现问题。
此错误是由以下行引起的...
mAsyncFacebookRunner.request("fql", paramaters,
new FQLRequestListener());
最佳答案
java.net.UnknownHostException
通常表示无法解析主机的 IP 地址,但实际原因可能因情况而异。如果代码实现正确(不管你使用的API是HttpUrlConnection还是DefaultHttpClient),还是断断续续的发生,很可能是老Android系统的DNS缓存和TTL管理相关的bug:
Issue 7904: Android does not support TTL and caches DNS result for 10 minutes
自 Android 4.1 起已修复,请参阅 InetAddress API Doc 中的额外注释:
DNS caching
In Android 4.0 (Ice Cream Sandwich) and earlier, DNS caching was performed both by InetAddress and by the C library, which meant that DNS TTLs could not be honored correctly. In later releases, caching is done solely by the C library and DNS TTLs are honored.
对于旧的 Android 版本,Android 建议调整 Java 级别的 DNS 属性 networkaddress.cache.ttl
和 networkaddress.cache.negative.ttl
,请参阅 old source code 中的 JavaDoc :
/**
* ... ...
*
* <h4>DNS caching</h4>
* <p>On Android, addresses are cached for 600 seconds (10 minutes) by default. Failed lookups are
* cached for 10 seconds. The underlying C library or OS may cache for longer, but you can control
* the Java-level caching with the usual {@code "networkaddress.cache.ttl"} and
* {@code "networkaddress.cache.negative.ttl"} system properties. These are parsed as integer
* numbers of seconds, where the special value 0 means "don't cache" and -1 means "cache forever".
*
* ... ...
*/
相关讨论:
- UnknownHostException in java (that too only sometimes)
- what is the purpose of Java level DNS caching?
如果您的目标是旧的 Android 版本,请尝试调整这两个属性,看看是否有任何不同。
关于java - Android UnknownHostException Facebook SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12607332/