0

The answer for checking internet was posted back in 2014 in this post: https://stackoverflow.com/a/27312494/12359431

However, in one of the answer, there is this piece of code

fun hasInternetConnection(): Single<Boolean> {
  return Single.fromCallable {
    try {
      // Connect to Google DNS to check for connection
      val timeoutMs = 1500
      val socket = Socket()
      val socketAddress = InetSocketAddress("8.8.8.8", 53)

      socket.connect(socketAddress, timeoutMs)
      socket.close()

      true
    } catch (e: IOException) {
      false
    }
  }
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
}

I have tried it by implementing the code above to my code at the bottom. However, it just crashes and I could not get any finding of the error as to why the app crash.

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    /* Initialise Azure Service Adapter */
    AzureServiceAdapter.Initialize(this)

    hasInternetConnection().subscribe{hasInternet->
                    /*Call database and check phone number*/
                    Log.i("Logger", "Connected")}

    /* Authentication */
    authUser()

}
}

This is my implementations

implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'

Is there anything I'm lacking or I shouldn't add to my MainActivity File? Or a clue as to why my kotlin app crash ?

Shreamy
  • 341
  • 2
  • 16
  • What stacktrace do you receive for this crash? – Christopher Mar 23 '20 at 06:42
  • @Christopher the error is : _E/AndroidRuntime: FATAL EXCEPTION: main Process: com.oslost.labex, PID: 921 java.lang.BootstrapMethodError: Exception from call site #1 bootstrap method at io.reactivex.rxjava3.android.schedulers.AndroidSchedulers.(AndroidSchedulers.java:33) at io.reactivex.rxjava3.android.schedulers.AndroidSchedulers.mainThread(AndroidSchedulers.java:44) _ – Shreamy Mar 23 '20 at 06:47

1 Answers1

0

That's because you cant call this on main Thread.

Check if you added Internet permission in Manifest.

 hasInternetConnection()
    .subscribeOn(Schedulars.io())
    .observeOn(AndroidSchedulars.mainThread()).subscribe{hasInternet->
                        /*Call database and check phone number*/
                        Log.i("Logger", "Connected")}
Jayanth
  • 5,954
  • 3
  • 21
  • 38
  • Hi Jayanth, if I can't call on the main Thread, is there any advice on where I would possibly call it. I'm still new to this thread things. Thanks :-) – Shreamy Mar 23 '20 at 06:48
  • 1
    Appearantly, you use `Schedulers.io()` for the network call. Just the subscriber runs on main thread, which looks ok too me. – Christopher Mar 23 '20 at 07:10
  • Please make sure you have given internet permission. – Jayanth Mar 23 '20 at 07:13
  • I have check that I have given permission. So ermm, where can I call the function within the activity ? – Shreamy Mar 23 '20 at 07:15
  • actually the code works fine. I tested on my machine. can you supply onError callback to the subscriber and post the log here – Jayanth Mar 23 '20 at 07:22
  • so we can get to know what problem you are facing – Jayanth Mar 23 '20 at 07:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210141/discussion-between-creamy-oreo-and-jayanth). – Shreamy Mar 23 '20 at 07:43