Here is the situation. My app runs fine, and is able to establish connections with URLs. BUT after a few hours of leaving the app running, all of a sudden the Facebook requests are giving me the following error.
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)
This error happens both on the app and the emulator. When I logout of my app and reconnect to Facebook the connections work again.
I should note: When I establish connections with my own server, no problem occurs.
This error is caused by lines such as the following...
mAsyncFacebookRunner.request("fql", paramaters,
new FQLRequestListener());
java.net.UnknownHostException
generally means the IP address of a host could not be resolved, although the actual cause may vary case-by-case. If the code is implemented properly (regardless of which API you use, HttpUrlConnection or DefaultHttpClient) and it is still happened intermittently, it is very likely a bug in old Android system related to DNS caching and TTL management:
Issue 7904: Android does not support TTL and caches DNS result for 10 minutes
This is fixed since Android 4.1, see the extra notes in 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.
For old Android version, Android suggests adjust Java level DNS properties networkaddress.cache.ttl
and networkaddress.cache.negative.ttl
, see JavaDoc in old source code:
/**
* ... ...
*
* <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".
*
* ... ...
*/
Related discussion:
- UnknownHostException in java (that too only sometimes)
- what is the purpose of Java level DNS caching?
Try adjusting those two properties and see if that makes any difference, if you are targeting on an old Android version.
I don't have any core conceptual knowledge about network connectivity and DNS caching and TTL management.
but the same problem occurred to me and at that time Just As a Workaround I have made few changes in the Facebook project's Util.java [The source project of the Facebook SDK] and then used it for my project. That two changes is just used from different stackoverflow answers.
In the function openUrl(String url, String method, Bundle params)
Replace this line,
HttpURLConnection conn =
(HttpURLConnection) new URL(url).openConnection();
with these lines,
try {
InetAddress i = InetAddress.getByName(url);
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
HttpURLConnection conn = (HttpURLConnection) new URL(url)
.openConnection();
conn.setConnectTimeout(50000);
And my problem was reduced.
It will help if you can post the code.....but, from my experience, calling httpClient.getConnectionManager().shutdown(); resolved this issue.