Android下载多个页面

时间:2014-06-02 11:51:03

标签: android android-listview android-asynctask downloading

我有问题。当我点击一个按钮时,我必须从网站(源)下载主页面。然后我必须解析源代码以获取其他链接。之后,我必须在列表视图中添加尽可能多的行,因为我在主页面源中找到了链接。最后我必须下载每个链接(再次来源)并解析它们。在下载/解析期间,我必须使用从这些来源获得的信息更新我的列表视图(例如进度条)。

但我该怎么做?

我以为我可以创建两个扩展AsyncTask的类。一个MainPageDownloader和一个PageDownloader。点击按钮即可启动MainPageDownloader。它下载主页面,在onPostExecute方法中,它将解析源并将行添加到列表中。然后它将启动PageDownloaders。

我试过了,但PageDownloaders将在列表尚未更新时启动。所以我无法更新每一行中的ProgressBar

我做错了什么?可能有一个完全不同的更好的方式吗?

以下是两个类:

public class MainPageDownloader extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {

        InputStream in = null;
        final int BUFSIZE = 1024;
        String result = "";

        try {
            URL url = new URL( params[0] );
            URLConnection con = url.openConnection();

            con.connect();
            in = con.getInputStream();

            byte[] data = new byte[BUFSIZE];
            while (in.read(data) != -1) {
                result += new String(data);
            }

        } catch (MalformedURLException e) {
            cancel(true);
        } catch (IOException e) {
            cancel(true);
        }

        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        List<String> links = new PageParser(result).getPageLinks();
        super.onPostExecute(result);

        int rows = listView.getChildCount();

        //Add items & update list
        for( int i = 0; i < links.size(); i++ )
            adapter.add( new Item() );

        for( int i = 0; i < links.size(); i++ ){
            new PageDownloader(rows+i).execute("http://www.mylink.com/" + links.get(i) );
        }

    }



}


public class PageDownloader extends AsyncTask<String, Integer, String>{

    ProgressBar p;
    int row;

    public PageDownloader(int row) {
        //p is null, I have to comment it out or the program will crash
        //p = (ProgressBar) listView.getChildAt( row ).findViewById( R.id.progressBar1 );
        this.row = row;
    }

    @Override
    protected String doInBackground(String... params) {
        InputStream in = null;
        final int BUFSIZE = 1024;
        String result = "";

        try {
            URL url = new URL( params[0] );
            URLConnection con = url.openConnection();

            con.connect();
            in = con.getInputStream();

            byte[] data = new byte[BUFSIZE];
            while (in.read(data) != -1) {
                result += new String(data);
            }

        } catch (MalformedURLException e) {
            cancel(true);
        } catch (IOException e) {
            cancel(true);
        }

        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        PageParser parser = new PageParser( result );

        //Somehow the list is updated now and the titles will change
        listItems.get(row).setTitle( parser.getTitle() );
        adapter.notifyDataSetChanged();

                    //Not Possible because p is always null
        //p.setVisibility( ListView.GONE ); 
    }



}

0 个答案:

没有答案