Y
Y
yuki2020-10-15 20:08:37
Java
yuki, 2020-10-15 20:08:37

Output a connection to the site in a separate thread?

Hello! It became interesting to parse a page from html into an application based on android 5.0+. Swears that everything is done in the main thread. Help to beat this situation so that the error disappears. How to transfer to another stream? And, if it's not too difficult, please provide a part of the code to eliminate misunderstanding

Code in Java

private final String[] arrDay = {"Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье"};
    private final String urlIVT = "https://www.ulstu.ru/schedule/students/part1/34.html";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadData();
    }

    private void loadData() {
        try {
            Document doc = Jsoup.connect(urlIVT).get();
            Elements firstWeek = doc.getElementsByTag("table");
            Toast.makeText(this, "Sucsessful loaded", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2020-10-15
Hasanly @azerphoenix

Hello!
It turns out that when you create an activity (in the onCreate() method), the loadData() method is called, which, using Jsoup, parses the data.
You can bring the loadData() method into a class that implements the Runnable interface and execute this method in a separate thread. You can also execute the method asynchronously.

F
foonfyrick, 2020-10-16
@foonfyrick

Document doc = Jsoup.connect(urlIVT).get(); this line gets the document from you in the form of the entire page of the site, it needs to be executed in a separate thread, the logs tell you about it. You can use Thread().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question