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:
For old Android version, Android suggests adjust Java level DNS properties
networkaddress.cache.ttl
andnetworkaddress.cache.negative.ttl
, see JavaDoc in old source code:Related discussion:
Try adjusting those two properties and see if that makes any difference, if you are targeting on an old Android version.
It will help if you can post the code.....but, from my experience, calling httpClient.getConnectionManager().shutdown(); resolved this issue.
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,
with these lines,
And my problem was reduced.