0

I am developing app which requires html parsing. So, I'm currently using jsoup in AsyncTaskLoader like this (example):

@Override
public Boolean loadInBackground() {
    try {
        Connection.Response response = Jsoup.connect(getContext().getString(R.string.url_login))
                .data("id", account_id, "password", account_password)
                .timeout(5000)
                .method(Connection.Method.POST)
                .execute();

        String cookie = response.cookie("JSESSIONID");

        Document document = Jsoup.connect(getContext().getString(R.string.url_schedule))
                .cookie("JSESSIONID", cookie)
                .get();

            Element table = document.select("table").first();
            if (table != null) {
                databaseHandler.openDatabase();
                databaseHandler.getDatabase().beginTransaction();
                try {
                    for (Element row : table.select("tr")) {
                        Elements columns = row.select("td");
                            addItem(columns, DatabaseHandler.getTableName());
                    }
                    databaseHandler.getDatabase().setTransactionSuccessful();
                } finally {
                    databaseHandler.getDatabase().endTransaction();
                }
                databaseHandler.closeDatabase();
            }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

This is just one page scrape and there is few of them. And I noticed that speed of it is not very good. So, I have been told that I should consider doing multithreading and parse all these pages in separate threads at the same time so it would be faster. Now I have a few questions:

  1. Should I still use AsyncTaskLoader or AsyncTask, or is there something else (better) for that solution ? I want to know what is the best practice for this thing.
  2. Can anyone guide me to tutorials / examples how to do multithreading in android ?

Thanks ;)

emilancius
  • 296
  • 1
  • 5
  • 15

1 Answers1

0

AsyncTask should work good for this. However, as per the docs for AsyncTask, it's default model is still "one task a a time" unless you use the executeOnExecutor method.

From http://developer.android.com/reference/android/os/AsyncTask.html :

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

You didn't say how many pages there were to parse, so just make sure you limit the number of simultaneous tasks to a reasonable number.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • Well, there always is 4 pages that has to be parsed when app loads, and there is 2 more, when user requests. So AsyncTask should be somewhat like the main background thread and I have to run other threads inside it (and if that is the idea, how should I tell to the AsyncTask thread if all the pages were parsed and I can close these child-background threads?)? Or I should make 6 separate tasks and run them all separately with this .exectureOnExecutor() ? P.S. Isn't there people that say AsyncTask is 'outdated' ? Or it's just a rumours ? – emilancius Aug 31 '14 at 19:58
  • 1
    @enVGuy - The fact that you are asking these questions shows a need to learn more about the fundamentals and foundations of writing multithreaded code, marshalling data betweeen threads, locks, etc.... I suggest the following book: [this book](http://amzn.com/1449364136) and [this one](http://amzn.com/0201310090) as well. – selbie Aug 31 '14 at 20:47