Android UnknownHostException Facebook SDK

后端 未结 3 1461
生来不讨喜
生来不讨喜 2021-01-05 21:55

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 reque

相关标签:
3条回答
  • 2021-01-05 22:40

    It will help if you can post the code.....but, from my experience, calling httpClient.getConnectionManager().shutdown(); resolved this issue.

    0 讨论(0)
  • 2021-01-05 22:49

    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.

    0 讨论(0)
  • 2021-01-05 22:51

    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.

    0 讨论(0)
提交回复
热议问题