0

I've import Jsoup into my project, everything is okay until this time. But as I trying to connect and URL, I got this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}:android.os.NetworkOnMainThreadException

I've added permission:

<uses-permission android:name="android.permission.INTERNET"/>

This line causes the error:

var doc = Jsoup.connect("https://google.com").get()

That's it. I've searched on stack overflow but I can not solve it.

shn
  • 79
  • 1
  • 8
  • Does this answer your question? [How to fix 'android.os.NetworkOnMainThreadException'?](https://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) – Edric Apr 06 '21 at 09:49

1 Answers1

0

Call it in the new Thread or Coroutine. For example:

Thread {
    var doc = Jsoup.connect("https://google.com").get()
}.run()
Yegorf
  • 400
  • 2
  • 11
  • Thanks but result is same. Nothing changed. – shn Apr 06 '21 at 10:46
  • Hmm, mb try to use async task - https://stackoverflow.com/questions/14247339/parsing-with-jsoup-throws-error-networkonmainthreadexception – Yegorf Apr 06 '21 at 10:57
  • Okay thanks. I made it somehow. Why we use thread to connect? – shn Apr 06 '21 at 13:02
  • @Furkan, because it is bad to weight the main thread with network calls. It must be called async to let main thread live normal life in this time :) – Yegorf Apr 06 '21 at 13:20
  • Why it is bad? Doesn't we already run all functions in main thread?(I'm not sure that main thread = onCreate). – shn Apr 06 '21 at 13:36
  • Main thread - is not only onCreate, of course. Main thread - its is everywhere, where you dont use another threads, if speak simple language. The general duty of this thread - its to display UI. (btw in android you can change UI ONLY FROM MAIN THREAD), in other case - you will get exception. Soo. If your main thread will be overwhelmed with tasks, your UI will freez, crash and so on. So it is very important to keep it "clean". – Yegorf Apr 06 '21 at 14:24
  • Thanks for your helps. But still hard to understand to me. – shn Apr 06 '21 at 15:24
  • 1
    Network calls can take a long time - think how long a webpage can take to respond in a browser (and that's when there isn't a problem!) If you perform a network call on the main thread, it blocks until you get a response. Your app can't do *anything else* on the main thread, including updating the display or responding to touches. At best that makes the app feel like it's stuttering, at worst it feels like it's completely stopped working (and Android will pop up the Application Not Responding message if it hangs for more than a couple of seconds) – cactustictacs Apr 06 '21 at 18:28
  • As far as I understand: onCreate method can not run a cycle because it doesn't respond of url connection. And it causes blockage. And by using thread we did it like parallel connection. Right? – shn Apr 06 '21 at 19:42
  • 1
    Yep, something like that :) – Yegorf Apr 07 '21 at 07:11