For my app (supporting Android 2.2+) I have to check HTML-code of a lot (approx 700) of different web-pages and retrieve a single name from each web-page. I have all the URL's stored in an array.
I now use a single Asynctask and iterate over the array with URLs like this:
(snippet from Asynctask's doinbackground)
publishProgress(urls.size());
int a = 0;
for(String code : urls) {
if(!running) return null;
try {
URL url = new URL(code);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
naam_codes.put(readStream(con.getInputStream(), true).get(0), code);
} catch (Exception e) {
running = false;
}
publishProgress(++a);
and readstream being:
BufferedReader reader = null;
ArrayList<String> html = new ArrayList<String>();
try {
reader = new BufferedReader(new InputStreamReader(in, Charset.forName("ISO-8859-1")));
if (snel){
//reading, matching and stuff
}
else {
//other reading, matching and stuff
}
}
} catch (IOException e) {
//pass
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
return null;
}
}
}
return html;
Now my problem is that it has to wait for one download+matching to finish before starting with a new one. It should be possible to speed this up, right? After monitoring for a bit the process doesn't seem to fully use the CPU nor internet-bandwidth(?). Should I instead of iterating inside one Asynctask, iterate on the UI-thread and execute multiple Asynctasks? If so, how?