1

I'm developing an Android application which periodically reports back to a data server via a REST API call. Whenever the app tries to send one of these reports, but no network channel (wifi or mobile data) is available, the app will create a wrapper for that task and put it in a queue that is attached to a BroadcastReceiver. The receiver listens for either android.net.wifi.STATE_CHANGE or android.net.conn.CONNECTIVITY_CHANGE and then tries to execute the enqueued task on the new connection. All of the authentication logic is handled here as well. The actual request POSTs are going out via the Retrofit library.

My problem is that, whenever network access "comes back", and the receiver tries to kick off all the tasks that are waiting, I always get back a java.net.UnknownHostException, stating that the host name of the data server has no associated address. It seems that, while the Android device knows that it's connected to a network, it still hasn't figured out where to get DNS from, and any requests that go out while it's in this state will fail.

D/Retrofit﹕ java.net.UnknownHostException: Unable to resolve host "[REDACTED]": No address associated with hostname
        at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
        at java.net.InetAddress.getAllByName(InetAddress.java:214)
        at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
        at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:266)
        at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:240)
        at com.squareup.okhttp.internal.http.RouteSelector.nextUnconnected(RouteSelector.java:156)
        at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:130)
        at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:312)
        at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:235)
        at com.squareup.okhttp.Call.getResponse(Call.java:262)
        at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:219)
        at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:192)
        at com.squareup.okhttp.Call.execute(Call.java:79)
        at retrofit.client.OkClient.execute(OkClient.java:53)
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
        at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
        at [REDACTED](Native Method)
        at [REDACTED]
        [etc]
  Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
  Caused by: libcore.io.ErrnoException: getaddrinfo failed: ENETUNREACH (Network is unreachable)

The root-cause exception (libcore.io.ErrnoException) claims that the network is unreachable, but I don't even try this execution until after I've received an intent from the Android OS specifically because the network is now, in fact, reachable.

My code for verifying wifi availability:

protected static boolean isWifiAvailable( WifiManager wifi )
{
    WifiInfo info = wifi.getConnectionInfo() ;
    if( info == null ) return false ;
    if( info.getNetworkId() == -1 ) return false ;
    if( info.getSupplicantState() != SupplicantState.COMPLETED )
        return false ;
    return true ;
}

My code for verifying mobile data availability:

protected static boolean isMobileDataAvailable( TelephonyManager tmgr )
{
    return( tmgr.getDataState() == TelephonyManager.DATA_CONNECTED ) ;
}

Question: Is there a more reliable signal — something I'm not checking in WifiInfo and TelephonyManager — that would indicate that the network channel is really, really ready? Or is this a race condition with no clear resolution?

1 Answer 1

1

Well as often happens I found a solution shortly after posting the question. It turns out that the mobile data check is sufficient... most of the time. The wifi check is the one that needed more work:

protected static boolean isWifiAvailable( WifiManager wifi )
{
    WifiInfo info = wifi.getConnectionInfo() ;
    if( info == null ) return false ;
    if( info.getNetworkId() == -1 ) return false ;
    if( info.getSupplicantState() != SupplicantState.COMPLETED )
        return false ;
    // Here are the crucial additions:
    DhcpInfo dhcp = wifi.getDhcpInfo() ;
    if( dhcp == null ) return false ;
    if( dhcp.ipAddress != info.getIpAddress() ) return false ;
    return true ;
}

By checking WifiInfo against DhcpInfo we finally have a conclusive test for whether the current wifi network is routable.

1
  • It's a god test but it isn't conclusive.
    – user207421
    Aug 28, 2015 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.