Android Question UnknownHostException: Unable to resolve host "example.com"

fbritop

Active Member
Licensed User
Longtime User
I have been dealing with an error, which I have attempt to shorten the posiblities. It only happens on Release. I have manage to simulate the situation.

The device, is connected through a mobile network and working fine. It does some requests every 30 seconds to a server in order to detect if there is connectivity. If I turn off mobile data, open the app, then it would respond with:

UnknownHostException: Unable to resolve host "api.llavemovil.com": No address associated with hostname, Response:

If I switch back the mobile data on the device, it still returns the same response (and for every other request, which involves other servers, diferent URI), also I´m sure that there is connectivity as I can open the servers URI in a browser, and everything else works fine.

If I restart the App it is ok. I think there is somewhere in OkHttp that hangs the whole thing, but I cannot figure out under which conditions. Has someone has this same issue?
 

josejad

Expert
Licensed User
Longtime User
Do you mean OkhttpUtils2? If you’re really using OkHttp you should change your code to OKHttpUtils2.
It would help if you show your code.
 
Upvote 0

fbritop

Active Member
Licensed User
Longtime User
Yes OkHttpUtils2, calling it by OkHttp is just a generic call. Using version 2.96

Code is just a standard fetch with a normal URI, this code is in B4XMainPage

B4X:
Sub Ping
    Try
        Dim httpJ As HttpJob
        httpJ.Initialize("jobPING", Me)
        
        httpJ.Download("https://status.llavemovil.com/")
        httpJ.GetRequest.Timeout=1500
    Catch
        Log(LastException)
    End Try
End Sub
    
Sub pingTimer_Tick
    Ping
End Sub
Sub JobDone (Job As HttpJob)
    xc.pdh
    If Job.JobName="jobPING" Then
        If Job.Success Then
            Main.kvs.Put("internetConnection", True)
        Else
            Main.kvs.Put("internetConnection", False)
        End If
        CallSub(Me, "internetAccess")
    End If
    Job.Release
End Sub
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Please, see tip 11 and adapt your code using wait for

 
Upvote 0

fbritop

Active Member
Licensed User
Longtime User
Thanks, but the error remains. There are about 50 diferent subs that use HttpJob, only 2 of them were still without Wait For. I change them but the problem remains.

#13 is just an elegant way, but it has nothing to do with connectivity errors.

B4X:
Sub pingTimer_Tick
    
    Try
        Dim jobLocal As HttpJob
        jobLocal.Initialize("", Me)
        jobLocal.Download("https://status.llavemovil.com/")
        jobLocal.GetRequest.Timeout=1500
        Wait For(jobLocal) JobDone(jobLocal As HttpJob)
        Main.kvs.Put("internetConnection", jobLocal.Success)
        jobLocal.Release
        CallSub(Me, "internetAccess")
    Catch
        Log(LastException)
    End Try
End Sub
 
Upvote 0

fbritop

Active Member
Licensed User
Longtime User
To whom it may help, I´ll try to explain the situation, and what did happen. The App I develop, what it does, is that it opens gates (relays) in community condos that are attached to an ESP32, subscribed to mdash.net. The problem comes, when houses that are near the main entrance, the user device tries to connect to their homes Wifi with a very weak signal, so it is very dificult to transmit data to the internet.

Given that problem, is that I asked @Erel how could I (if the user forced it) redirect internet trafic only by the mobile network, which came with a solution (that can't be used in B4I)

This solution gives us a Network ID, when it request a PreferMobileRouting sub. When the mobile network gets disconnected (modem reset I think), I put a broadcastReceiver to get this event, and then I call again PreferMobileRouting, because the ID of PreferMobileRouting has changed. This solved the whole issue.

Broadcast Receiver:
B4X:
Sub BroadcastReceiver_OnReceive (Action As String, i As Object)
    Dim retIn As Intent
    retIn = i
    Dim IsConnected As String
    Dim TheType As String
 
    If Action = "android.net.conn.CONNECTIVITY_CHANGE" Then

        Dim jo As JavaObject = retIn
        Dim NetworkInfo As JavaObject = jo.RunMethod("getParcelableExtra", Array("networkInfo"))
        TheType = NetworkInfo.RunMethod("getTypeName", Null)
        IsConnected = NetworkInfo.RunMethod("getState", Null)
        If TheType.ToUpperCase="MOBILE" And IsConnected.ToUpperCase="CONNECTED" Then CallSub2(Main, "PreferMobileRouting", True)
    End If
    BroadCast.AbortBroadcast
End Sub

How to suggest a Mobile Connection Route for the App:
B4X:
Sub PreferMobileRouting(prefer As Boolean) As ResumableSub
    Dim p As Phone
    If p.SdkVersion >= 21 Then
        
'    Else
'        builder.RunMethod("addTransportType", Array(0))
'        builder.RunMethod("addTransportType", Array(1))
'        builder.RunMethod("addCapability", Array(12)) 'NetworkCapabilities.NET_CAPABILITY_INTERNET
'        Log("NET_CAPABILITY_INTERNET")

        
        If prefer Then
            Dim ctxt As JavaObject
            ctxt.InitializeContext
        
            Dim manager As JavaObject = ctxt.RunMethod("getSystemService", Array("connectivity"))
        
            Dim builder As JavaObject
            builder.InitializeNewInstance("android.net.NetworkRequest.Builder", Null)
            builder.RunMethod("addTransportType", Array(0)) 'NetworkCapabilities.TRANSPORT_CELLULAR
            Log("TRANSPORT_CELLULAR")
        
            Dim event As JavaObject
            event.InitializeNewInstance(Application.PackageName & ".main$NetworkCallback", Null)
            manager.RunMethod("requestNetwork", Array(builder.RunMethod("build", Null), event))
            Wait For network_available (Network As Object)
            Log("Network found.")
            Log(Network) '<--THIS ID CHANGES EVERY TIME THE MODEM RESTARTS OR RESETS
            Dim cm As JavaObject
            Return (cm.InitializeStatic("android.net.ConnectivityManager").RunMethod("setProcessDefaultNetwork", Array(Network)))
        End If
    End If
    Return False
End Sub
 
Upvote 0
Top