RSS在Android中加载太长时间

时间:2012-08-12 23:49:02

标签: android xml rss xml-parsing

我正在使用AsyncTask从RSS提要和DO​​M解析器以及ArrayAdapter获取数据。

我注意到RSS源需要很长时间来加载并在屏幕上显示数据,起初我认为因为文章的数量很大但是当我限制for循环只阅读3篇文章时,它仍然花费太长时间展示文章!!

下面是我用来解析数据和主要活动的完整代码

主要活动代码:

public class NewsMainActivity extends Activity implements OnItemClickListener {
    /** Called when the activity is first created. */

    ListView listview;
    List<NewsBean> arrayList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);

        initComponents();

        if (Utils.isNetworkAvailable(this)) {
            new MyRssReadTask()
                    .execute("feed_URL");

        } else {
            Toast.makeText(this, "Please check your internet connection", Toast.LENGTH_SHORT)
                    .show();
        }

    }

    private void initComponents() {
        listview = (ListView) findViewById(android.R.id.list);
        listview.setOnItemClickListener(this);

    }

    class MyRssReadTask extends AsyncTask<String, Void, Void> {
        ProgressDialog waitingDialog;

        @Override
        protected void onPreExecute() {
            waitingDialog = new ProgressDialog(NewsMainActivity.this);
            waitingDialog.setMessage("Loading articles, Please wait...");
            waitingDialog.show();
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(String... urls) {

            arrayList = new NewsParser().getData(urls[0]);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            waitingDialog.dismiss();
            setDataToListView();
            super.onPostExecute(result);
        }
    }

    protected void setDataToListView() {
        if (null != arrayList && arrayList.size() != 0) {
            NewsRowAdapter objNewsRowAdapter = new NewsRowAdapter(
                    NewsMainActivity.this, R.layout.row, arrayList);

            listview.setAdapter(objNewsRowAdapter);
        } else {
            showToast("There was an error to get data, please try again later.");
        }

    }

    void showToast(String msg) {
        Toast.makeText(NewsMainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        final NewsBean objBean = (NewsBean) arrayList.get(position);
        Intent intent = new Intent(NewsMainActivity.this,
                NewsDetail.class);

        intent.putExtra("title", objBean.getTitle());
        intent.putExtra("description", objBean.getDescription());
        //intent.putExtra("pubdate", objBean.getPubDate());
        //intent.putExtra("creator", objBean.getCreator());
        intent.putExtra("link", objBean.getLink());

        startActivity(intent);
    }

新闻解析器

public class NewsParser {


    private List<NewsBean> arrayListPasre;

    public List<NewsBean> getData(String _url) {
        try {

            arrayListPasre = new ArrayList<NewsBean>();
            URL url = new URL(_url);
            URLConnection con = url.openConnection();

            System.out.println("Connection is : " + con);

            System.out.println("Connection InputStream is : "
                    + con.getInputStream());

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            System.out.println("Reader us now : " + reader);

            String inputLine;
            String fullStr = "";
            while ((inputLine = reader.readLine()) != null)
                fullStr = fullStr.concat(inputLine + "\n");

            InputStream istream = url.openStream();

            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();

            System.out.println("Builder : " + builder);

            Document doc = builder.parse(istream);

            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("item");

            System.out.println("nlist " + nList);

            for (int temp = 0; temp < 3; temp++) {

                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    NewsBean objBean = new NewsBean();

                    objBean.setTitle(getTagValue("title", eElement));
                    objBean.setDescription(getTagValue("description", eElement));
                    objBean.setLink(getTagValue("link", eElement));
                    arrayListPasre.add(objBean);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return arrayListPasre;
    }

    private String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
                .getChildNodes();

        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();

    }
}

任何建议我都会感激不尽! :)

1 个答案:

答案 0 :(得分:1)

您不需要一次解析整个rs。你可以部分地做到这一点。例如,解析文档并获取rss项目计数,然后仅为3或5或10个项目调用异步任务。在onPostExecute()中将结果添加到适配器并调用notifydatasetchanged()。