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
im using this code it is working perfectly but the issue is if i connect to wifi and there is no internet access it also show me there is internet connection.
how i can get rid of it?
B4X:
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
Not in my computer now, but please, just search for okhttputils2 tutorial.
There's a piece of code
B4X:
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
If j.sucess is false, then there's some Internet connection problem (or Google is down, very improbable)
** 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 **
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
- Take a look to For new(er) members: How to post a question/issue
- Don't post code or error as images. Use [code]... your code or error here... [/code] tags
- Your code is underlined, hover over your code and you will get some clues about some problem in the code. (remove out.TestInternet and use just TestInternet)
- You better upload a small project testing the internet connection. It will be easier to help you with your error
Why i got this Error Resumable subs return type must be ResumableSub (or none) How i can correct it ?
B4X:
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
Actually, you don't need to check WiFi connection.
Try this code in Sub Button1_Click
B4X:
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
As Erel said above, the surest way is to try sending your request and handle it properly if it fails.
The alternatives all have problems, as far as I know:
- Checking for wifi ignores mobile connections.
- Checking for Internet (wifi, mobile, whatever) is better, but you may not be able to reach the desired server (e.g. a ping will fail).
- In some cases, you can ping a server but can't reach it with a real request. (This one is really annoying to me.)
Yo uso esto, que creo recordar lo puso Erel en algún post:
Dejo el codigo previo que usaba:
B4X:
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