首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简汤Android解析

简汤Android解析
EN

Stack Overflow用户
提问于 2016-02-21 23:44:51
回答 1查看 123关注 0票数 1

我在解析一个网页..。现在等我说完了..。我看到我在做一些非常愚蠢的事情,所以如果有人能向我指出这一点,请告诉我:)什么才是正确的方向。

我有一个Android应用程序,它使用的是Jsoup,它工作的很好,但是它非常慢!我知道为什么..。因为基本上onCreate我有20,30次getElement请求.

代码语言:javascript
复制
 private class Task extends AsyncTask<Void, Void, Void>{
    String linkText;
    @Override
    protected Void doInBackground(Void... params) {
        Initdata();

        return null;
    }
    @Override
    protected void onPostExecute(Void param) {

        mProgressBarHandler.hide();           

        redraw();
        inflatedView.invalidate();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressBarHandler.show();
    }
}

在Initdata()方法中,我有20-30个Jsoup请求。即使使用AsyncTask,它也很慢,现在唯一的区别是我没有阻塞UI线程,.这很好,但我需要对这些元素的解析进行“优化”。

代码语言:javascript
复制
private void Initdata(){

    loadImages();
    players = new String[] {util.GetElement("div.item-2:first-child", "http://www.istinomer.rs/", 0),
            util.GetElement("div.item-2:nth-child(2)", "http://www.istinomer.rs/", 0),
            util.GetElement("div.item-2:nth-child(3)", "http://www.istinomer.rs/", 0),
            util.GetElement("div.item-2:nth-child(4)","http://www.istinomer.rs/",0),
            util.GetElement("div.item-2:nth-child(5)","http://www.istinomer.rs/",0),
            util.GetElement("div.item-2:nth-child(6)","http://www.istinomer.rs/",0),
            util.GetElement("div.item-2:nth-child(7)","http://www.istinomer.rs/",0),
            util.GetElement("div.item-2:nth-child(8)","http://www.istinomer.rs/",0),
            util.GetElement("div.item-2:nth-child(9)","http://www.istinomer.rs/",0),
            util.GetElement("div.item-2:nth-child(10)","http://www.istinomer.rs/",0)
    };
 vestiDescription1 = util.GetElement("div.item-big h2", "http://www.istinomer.rs/", 0) + System.getProperty("line.separator")
            + util.GetElement("div.item-big h3","http://www.istinomer.rs/",0);

    vestiDescription2 = util.GetElement("div.grid-8 h2 a", "http://www.istinomer.rs/", 0) + System.getProperty("line.separator")
            + util.GetElement2("div.grid-8 h3","http://www.istinomer.rs/",0);

    vestiDescription3 = util.GetElement(
            "div.gd-container-1:nth-child(6) > div:nth-child(4) > div:nth-child(1) > div:nth-child(4) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);

    vestiDescription4 = util.GetElement(
            "div.gd-container-1:nth-child(6) > div:nth-child(4) > div:nth-child(1) > div:nth-child(5) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);

    vestiDescription5 = util.GetElement(
            "div.gd-container-1:nth-child(6) > div:nth-child(4) > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);

    vestiDescription6 = util.GetElement(
            "div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(2) > h2:nth-child(4)", "http://www.istinomer.rs/", 0) + System.getProperty("line.separator")
            + util.GetElement2("div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(2) > h3:nth-child(5)","http://www.istinomer.rs/",0);

    vestiDescription7 = util.GetElement(
            "div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);

    vestiDescription8 = util.GetElement(
            "div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(4) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);

    vestiDescription9 = util.GetElement(
            "div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(5) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);

    vestiDescription10 = util.GetElement(
            "div.gd-container-1:nth-child(6) > div:nth-child(5) > div:nth-child(1) > div:nth-child(6) > div:nth-child(2) > h3:nth-child(2)", "http://www.istinomer.rs/", 0);
currency = new String[]{
            vestiDescription1,
            vestiDescription2,
            vestiDescription3,
            vestiDescription4,
            vestiDescription5,
            vestiDescription6,
            vestiDescription7,
            vestiDescription8,
            vestiDescription9,
            vestiDescription10
    };

public String GetElement(String Element, String site, int mode) {
    try {

        Elements newsHeadlines = null;

        if (mode == 0) {
            Document doc = Jsoup.connect(site).timeout(600000).get();
            newsHeadlines = doc.select(Element);
        }
        //1 gets link from class
        else if (mode == 1) {
            Document doc = Jsoup.connect(site).timeout(600000).get();
            String link = doc.select(Element).toString();
            return link;
        }

        //Log.d("TMS", "Data is " + html2text(newsHeadlines.toString()));

        String returnData = html2text(newsHeadlines.toString());
        return returnData;
    }
    catch (Exception e) {
        Log.d("TMS", "EXCEPTION GetElement: " + Element);
        e.printStackTrace();
        return "Error";
    }

你知道我该怎么加速吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-22 00:32:55

在每次调用GetElement时,您都请求同一个文档进行反复解析!当然很慢!

相反,对JSoup进行一次调用以获取文档,然后使用它返回的文档对象对该文档进行所有查询。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35543629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档