Sub TestConnessione As ResumableSub
'Test Connection
Wait For (oUt.TestInternet) Complete (Connesso As Boolean)
If Not(Connesso) Then
xui.MsgboxAsync($"Non hai connessione internet! ${CRLF}L'applicazione verrà chiusa."$,"Attenzione")
Wait For msgbox_result
'My sub of exit
End If
Return True
End Sub
Public Sub TestInternet() As ResumableSub
Dim Connected As Boolean = False
Dim Job As HttpJob
Job.Initialize("Job", Me)
Job.Download("https://www.google.it")
Wait For(Job) JobDone(Job As HttpJob)
Job.Release
If Job.Success Then
Connected = True
Exit
End If
Sleep(1000)
Return Connected
End Sub
Sub Button1_Click
If CheckInternetConnection = True Then
MsgboxAsync("Connected","Yes")
Else
MsgboxAsync("Not connected","No")
End If
End Sub
Sub CheckInternetConnection As Boolean
Dim jo As JavaObject
jo.InitializeContext
Dim cm As JavaObject = jo.RunMethod("getSystemService", Array("connectivity"))
Dim activeNetwork As JavaObject = cm.RunMethod("getActiveNetworkInfo", Null)
If activeNetwork.IsInitialized Then
Return activeNetwork.RunMethod("isConnected", Null)
Else
Return False
End If
End Sub
how i can get rid of it?
make a request with OkHttpUtils2 and if it fails then tell the user that there is a connection problem
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://www.google.com")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
End If
j.Release
** Activity (main) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
*** Receiver (httputils2service) Receive (first time) ***
httputils2service_service_create (java line: 185)
java.lang.ExceptionInInitializerError
at okhttp3.internal.platform.Platform$Companion.findAndroidPlatform(Platform.kt:219)
at okhttp3.internal.platform.Platform$Companion.findPlatform(Platform.kt:212)
at okhttp3.internal.platform.Platform$Companion.access$findPlatform(Platform.kt:169)
at okhttp3.internal.platform.Platform.<clinit>(Platform.kt:170)
at okhttp3.OkHttpClient.<init>(OkHttpClient.kt:237)
at okhttp3.OkHttpClient$Builder.build(OkHttpClient.kt:1069)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.Initialize(OkHttpClientWrapper.java:94)
at b4a.example.httputils2service._service_create(httputils2service.java:185)
at b4a.example.httputils2service._receiver_receive(httputils2service.java:146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at b4a.example.httputils2service.onReceive(httputils2service.java:42)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2407)
at android.app.ActivityThread.access$1700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Expected Android API level 21+ but was 19
at okhttp3.internal.platform.AndroidPlatform.<clinit>(AndroidPlatform.kt:153)
... 25 more
** Activity (main) Resume **
But have you tried the code I posted? It does exactly what you want to doI'm not familiar with this code could you please
write down it here just small example
I test it with ErrorI use this
B4X:Sub TestConnessione As ResumableSub 'Test Connection Wait For (oUt.TestInternet) Complete (Connesso As Boolean) If Not(Connesso) Then xui.MsgboxAsync($"Non hai connessione internet! ${CRLF}L'applicazione verrà chiusa."$,"Attenzione") Wait For msgbox_result 'My sub of exit End If Return True End Sub Public Sub TestInternet() As ResumableSub Dim Connected As Boolean = False Dim Job As HttpJob Job.Initialize("Job", Me) Job.Download("https://www.google.it") Wait For(Job) JobDone(Job As HttpJob) Job.Release If Job.Success Then Connected = True Exit End If Sleep(1000) Return Connected End Sub
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Button1_Click
If CheckWiFiInternetConnection Then
Log("WiFi connected with Internet access")
Else
Log("No Internet access over WiFi")
End If
End Sub
Sub CheckWiFiInternetConnection As Boolean
' Check if connected to WiFi first
Dim jo As JavaObject
jo.InitializeContext
Dim cm As JavaObject = jo.RunMethod("getSystemService", Array("connectivity"))
Dim activeNetwork As JavaObject = cm.RunMethod("getActiveNetworkInfo", Null)
If activeNetwork.IsInitialized Then
Dim networkType As Int = activeNetwork.RunMethod("getType", Null)
If networkType = 1 Then ' 1 means WiFi
If activeNetwork.RunMethod("isConnected", Null) Then
' Now check if there's Internet access
If CheckInternetAccess = True Then
Return True
End If
End If
End If
End If
Return False
End Sub
' This method tries to reach a known website to verify internet access
Sub CheckInternetAccess As Boolean '<---- ' Error (Resumable subs return type must be ResumableSub (or none) )
Try
Dim job As HttpJob
job.Initialize("", Me)
job.Download("http://clients3.google.com/generate_204") ' Lightweight Google URL for connectivity check
Wait For (job) JobDone(job As HttpJob)
If job.Success Then
Log("Internet Access Available")
job.Release
Return True
Else
Log("No Internet Access")
job.Release
Return False
End If
Catch
Log("Error in checking internet access")
Return False
End Try
End Sub
1.Define CheckInternetAccess as ResumableSubWhy i got this Error
Resumable subs return type must be ResumableSub (or none)
How i can correct it ?
Sub CheckInternetAccess as ResumableSub
Wait For(CheckInternetAccess) Complete (Result As Boolean)
Return Result
Main - 40: Syntax error.
Main - 38: Cannot cast type: {Type=ResumableSub,Rank=0, RemoteObject=True} to: {Type=Boolean,Rank=0, RemoteObject=False}
Sub Button1_Click
Wait For(CheckWiFiInternetConnection) Complete (Result As Boolean)
'Or
'Wait For(CheckInternetAccess) Complete (Result As Boolean)
If Result Then
Log("WiFi connected with Internet access")
Else
Log("No Internet access over WiFi")
End If
End Sub
public Sub GetCheckConexionInternetV2() As Boolean
Dim server As ServerSocket 'NETWORK LIBARARY
server.Initialize(0,"")
If server.GetMyIP.StartsWith("127.0.0.1" ) Then
Return False
Else
Return True
End If
End Sub
public Sub GetCheckConexionInternetDeprecated() As ResumableSub
Dim Rt As Boolean = True
Try
Dim j As HttpJob 'OkHttpUtiles2
j.Initialize("", Me) 'name is empty as it is no longer needed
j.Download("http://www.google.es")
j.GetRequest.Timeout = 3000
Log(j.GetRequest.Timeout)
Wait For (j) JobDone(j As HttpJob)
Rt = j.Success
j.Release
Catch
Log(LastException)
End Try
Return Rt
End Sub