0

i want to download the html content of a website using asynctask, and use regex to manipulate the source code to get what i need and finally i want to display those result in a list view. this is my code it doesnt show error but when i run it on my emulator the app crashes please guys i need assistant this is really important

public class DownloadTask 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 in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView myLV = (ListView)findViewById(R.id.teamLV);
    ArrayList<String> clubName = new ArrayList<String>();

    DownloadTask task = new DownloadTask();
    String result = null;
    try {
        result = task.execute("https://www.premierleague.com/clubs").get();
        //Log.i("Content of URL", result);
        System.out.println(result);
        Pattern p = Pattern.compile("class=\"clubName\">(.*?)<");
        Matcher m = p.matcher(result);

        while(m.find()){
            System.out.println(m.group(1));
            clubName.add(m.group(1));
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, clubName);
            myLV.setAdapter(arrayAdapter);
        }


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

}

1 Answers1

0

try adding onPostExecute after doInBackground. then remove the part from execute.get(). The get method makes the UI blocking (which kinda makes the asynctask pointless).

also preset your adapter instead of creating a new one each time you do a loop. you might need a custom adapter (depending on what you need).

@Override
protected void onPostExecute(String result) {
    //Log.i("Content of URL", result);
    System.out.println(result);
    Pattern p = Pattern.compile("class=\"clubName\">(.*?)<");
    Matcher m = p.matcher(result);

    while(m.find()){
        System.out.println(m.group(1));
       //  clubName.add(m.group(1));

        adapter.add (m.group(1));

    }
}
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
  • its outputting the result i want but how do i populate list view with the result – abdulmajid adams Aug 08 '16 at 03:16
  • move the adapter codes (new and setAdapter) to before calling the asyncTask. it should work. more information about adding items in a list view can be found here : http://stackoverflow.com/questions/4540754/dynamically-add-elements-to-a-listview-android. – Angel Koh Aug 08 '16 at 08:05
  • thanks so much for ur reply, i still couldnt get it to display though... – abdulmajid adams Aug 08 '16 at 13:29