I could somehow "force" to use a type of connection in case both are connected, currently I have a problem with an application that if the cellular connection is active takes the ip of this network and not the wifi network that is the ip I need to take.
Hello
@enrfortin, if you want to activate WiFi or those types of functions, I believe I have read in the forum that Android is becoming more and more restrictive and does not allow it.
See this post:
Hi All Is there a way to switch the WiFi On or Off programmable in Android 11.. Thx
www.b4x.com
My function just tries to find out if there is a connection and what type. I use it
BEFORE to send GET or POST queries to an API, I leave you the example.
[CODE lang="b4x" title="GetJSONData" highlight="3"]Public Sub GetJSONData(strLink As String) As ResumableSub
Dim Result As String
If mdlFunctions.CheckInternet.blnIsOnline Then
Dim j As HttpJob
Try
j.Initialize("", Me)
j.Download(strLink)
j.GetRequest.SetHeader("Content-Type","application/json")
If mdlFunctions.mapToken.IsInitialized And mdlFunctions.mapToken.Size > 0 Then
j.GetRequest.SetHeader("UserId", mdlFunctions.mapToken.Get("id"))
j.GetRequest.SetHeader("UniqueKey", mdlFunctions.mapToken.Get("key"))
j.GetRequest.SetHeader("Authorization", mdlFunctions.mapToken.Get("token"))
End If
If mdlFunctions.mapHeaders.IsInitialized And mdlFunctions.mapHeaders.Size > 0 Then
For Each strKey As String In mdlFunctions.mapHeaders.Keys
j.GetRequest.SetHeader(strKey, mdlFunctions.mapHeaders.Get(strKey))
Next
End If
j.GetRequest.Timeout = 60000
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Result = j.GetString
Else
Result = j.ErrorMessage
End If
Catch
Result = LastException.Message
Log(LastException)
End Try
j.Release
End If
Return Result
End Sub[/CODE]
[CODE lang="b4x" title="PostJSONData" highlight="3"]Public Sub PostJSONData(strRun As String, mapData As Map) As ResumableSub
Dim Result As String
If mdlFunctions.CheckInternet.blnIsOnline Then
Dim JSONGenerator As JSONGenerator
Dim JSONString As String
JSONGenerator.Initialize(mapData)
Try
Dim j As HttpJob
j.Initialize("", Me)
JSONString = JSONGenerator.ToString
j.PostString(mdlFunctions.strRemoteHost & strRun, JSONString)
j.GetRequest.SetContentType("application/json")
If mdlFunctions.mapToken.IsInitialized And mdlFunctions.mapToken.Size > 0 Then
j.GetRequest.SetHeader("UserId", mdlFunctions.mapToken.Get("id"))
j.GetRequest.SetHeader("UniqueKey", mdlFunctions.mapToken.Get("key"))
j.GetRequest.SetHeader("Authorization", mdlFunctions.mapToken.Get("token"))
End If
If mdlFunctions.mapHeaders.IsInitialized And mdlFunctions.mapHeaders.Size > 0 Then
For Each strKey As String In mdlFunctions.mapHeaders.Keys
j.GetRequest.SetHeader(strKey, mdlFunctions.mapHeaders.Get(strKey))
Next
End If
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Result = j.GetString
Else
Result = j.ErrorMessage
End If
Catch
Result = LastException.Message
Log(LastException)
End Try
j.Release
End If
Return Result
End Sub[/CODE]
I have some variables of type Map (mapToken and mapHeaders), which I fill with the information that is required, both in the GET and in POST.
As you can see on line 3 in both cases, check beforehand, is there internet?
In the following example code, I query for the current version of the database
[CODE lang="b4x" title="Example to load DB"]Public Sub LoadDB(strTable As String) As ResumableSub
Dim strURL As String = ""
Select Case strTable
Case "get_db_info"
strURL = mdlFunctions.strRemoteHost & "db_info"
End Select
Wait For(clsFunct.GetJSONData(strURL)) Complete (strResult As String)
Return strResult
End Sub[/CODE]
I made the code prior to paying the $99 from Apple and
@Erel's service to be able to remotely compile the application. Now I find myself with these issues that should be simple but in reality, they are not.