android – HTML Parsing With AsyncTask makes the main thread slow-ThrowExceptions

Exception or error:

I am trying to download an HTML source and extracting a text in order to use it. But html is not well coded it’s in a table and I couldn’t think different way to extract it and I am using regex with pattern and matcher to extract it. I am doing download and parsing tasks 2 times. First task is doing great without any problem but when I try to do second task it skips the frames and saying you are doing too much on main thread but these operations in the AsyncTask.
My AsyncDownload task

public class DownloadTheCoursesTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        String result = "";
        URL url;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream input = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(input);
            int data = reader.read();

            while (data != -1) {
                char current = (char) data;
                result += current;
                data = reader.read();
            }
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

First parsing task which works perfectly

public void getTheCourses() {
    DownloadTheCoursesTask coursesTask = new DownloadTheCoursesTask();
    String result = "";

    try {
        result = coursesTask.execute("http://www.sis.itu.edu.tr/tr/ders_programlari/LSprogramlar/prg.php").get();
        coursesResult = new ArrayList<String>();
        Pattern firstPattern = Pattern.compile("<option  value=\"(.*?)\">");
        Matcher firstMatcher = firstPattern.matcher(result);

        while (firstMatcher.find()) {
            coursesResult.add(firstMatcher.group(1));
        }

    }catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

Second parsing task which crashes my app and says skipping frames

public void getTheQuota(){
    DownloadTheCoursesTask quotaTask = new DownloadTheCoursesTask();
    String result = "";
    try {
        result = quotaTask.execute("http://www.sis.itu.edu.tr/tr/ders_programlari/LSprogramlar/prg.php?fb=BEB").get();
        Pattern p = Pattern.compile("<html><head>(.*?)</html>");
        Matcher m = p.matcher(result);
        while (m.find()) {
            quotaResult.add(m.group(1));
        }

    }catch (InterruptedException e){
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

Many people says that use jSoup but my first task is running perfectly. What can I do about this?

How to solve:

Do not use the .get() member to execute an AsyncTask as then it will run on the main thread.

Do without and handle the result of doInBackground() in onPostExecute().

So your first task is wrong already too.

Leave a Reply

Your email address will not be published. Required fields are marked *