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:
- 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.
- Can anyone guide me to tutorials / examples how to do multithreading in android ?
Thanks ;)