1

我正在尝试使用 Jsoup lib 解析 html。一切都很完美,但有些东西没有显示出来。
代码:

protected ArrayList<Order> doInBackground(String... urls) {

        listItems.clear();
        myAdapterDouble.notifyDataSetChanged(); 
        String url = null;

        try {
            Document doc = Jsoup.connect(URL).timeout(0).userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").get();

            Elements days = doc.select("div.day_now");
            for (Element day : days) {
                dd = day.select("div.tooltip");
                for (Element d : dd) {

                    title = d.select("td.tooltip_title h4").text();
                    time = d.select("td.tooltip_info h4").text();
                    img = d.select("td.tooltip_desc img[src]");

                    Order o = new Order();
                    o.setLink(URL + img.attr("src"));
                    o.setTextName(title);
                    o.setTextTime(time
                            .replace("on", getResources().getString(R.string.on))
                            .replace("at", getResources().getString(R.string.at))
                            .replace("Ep:", getResources().getString(R.string.episode))
                            .replace("Final", getResources().getString(R.string.final_ep)));
                    o.setDetailsUrl(URL + url);   //set urls text in list
                    listItems.add(o);
                }

                Elements links = day.select("h3");
                for (Element link : links) {
                    url = link.select("a").attr("href");  // parse page urls
                    System.out.println(url);    //display urls in LogCat                    
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return listItems;
    }

在 LogCat 中,我看到了我在上面的代码中解析的 url

01-20 12:13:17.671: I/System.out(23390): /show/678/AKB0048_next_stage
01-20 12:13:17.671: I/System.out(23390): /show/668/Battle_Spirits%3A_Sword_Eyes
01-20 12:13:17.671: I/System.out(23390): /show/694/Beast_Saga
01-20 12:13:17.671: I/System.out(23390): /show/660/Cross_Fight_B-Daman_eS

但是这些链接没有显示在屏幕上,而是我得到空值。
我究竟做错了什么?
谢谢。

4

1 回答 1

1

目前您没有添加urllistItems. 更改您的代码以获取 url:

           ArrayList<Order> newarraylist=new ArrayList<Order>; 
           Elements links = day.select("h3");
           int urlcount=0;
           for (Element link : links) {
                url = link.select("a").attr("href");  // parse page urls
                System.out.println(url);    //display urls in LogCat 
              if(urlcount < listItems.size()){
                Order o = (Order)listItems.get(urlcount);
                o.setDetailsUrl(URL + url);   //set urls text in list
                newarraylist.add(o);
              }
              urlcount++;
            }

现在newarraylist从而doInBackground不是返回listItems

于 2013-01-20T06:40:16.737 回答