I am parsing html using Jsoup for Android, using AsyncTask.
I use it for a long time, but today I have a problem with parsing multiple pages that they relevants about adapter for RecyclerView, specific: https://www.hi.com?page=1 ; and https://www.hi.com?page=2 ,...
Count of page is not small, it is bigger than 100.
How can I parsing all of them but have to adapt nature of AsyncTask which like a queue, wait one until it done and start a new one.
I tried with using for for (int i=1;i<=countPage;i++) { //do new AsyncTask with new String that String change by the last word present by count ( ...?page=" + String.valueOf(countPage)... }
But it does not work.
I tried Thread , set AsynTask inside until getStatus=AsyncTask.Status==Done and new Parse, but till not working.
I tried using using ThreadPool, but till not working. I dont know how to fix this problem.
I dont wanna list all 100 links for pages...
Can someone help me? Thank alots. Have a nice day.
-
How about Volley?OneCricketeer– OneCricketeer2017-04-16 06:37:05 +00:00Commented Apr 16, 2017 at 6:37
-
Thank for replying. But maybe it not contains separating task in Asynctask like queue that I said, coz of I using multithreading inside a loop. :(Nguyễn Ngọc Tân– Nguyễn Ngọc Tân2017-04-16 06:45:07 +00:00Commented Apr 16, 2017 at 6:45
-
Could you please elaborate on what you want to achieve and what you've tried so far? Especially what you mean with "How can I parsing all of them but have to adapt nature of AsyncTask which like a queue, wait one until it done and start a new one".aha– aha2017-04-16 10:31:36 +00:00Commented Apr 16, 2017 at 10:31
-
@aha : Sorry for confusing you. In particular, AsyncTask is inside any for loop or for any while loop, so when you want to use the new AsyncTask in the new loop, wait for the old AsyncTask to complete, just like the queue.Nguyễn Ngọc Tân– Nguyễn Ngọc Tân2017-04-16 11:26:33 +00:00Commented Apr 16, 2017 at 11:26
-
@aha But, for any while or for loop, there will be no waiting time for the current AsyncTask to execute, and it goes straight to test whether the changed condition is still valid for repeating or not, so the old AsyncTask And the new AsyncTask collided.Nguyễn Ngọc Tân– Nguyễn Ngọc Tân2017-04-16 11:26:48 +00:00Commented Apr 16, 2017 at 11:26
|
Show 2 more comments
1 Answer
To execute an AsyncTask multiple times concurrently, you have to use the variant with the executor and create multiple instances:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (String param : new String[] {"foo", "bar", "baz"}) {
new SomeTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, param);
}
}
static class SomeTask extends AsyncTask<String, Long, String> {
@Override
protected String doInBackground(String... params) {
// do your background processing
return null;
}
}
}
Personally, I prefer RxJava. It's much more pleasant to use. The equivalent code in RxJava:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Observable.just("foo", "bar", "baz")
.map(new Function<String, String>() {
@Override
public String apply(String s) throws Exception {
// Background processing
return null;
}
})
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
// display result
}
});
}
}