Beispiel #1
0
  /**
   * 解析板块列表
   *
   * @param content
   * @return
   */
  public static List<PlateGroup> parsePlateGroupList(String content) {
    List<PlateGroup> groups = new ArrayList<PlateGroup>();
    Document document = Jsoup.parse(content);
    document.setBaseUri(Constants.BASE_URL);
    Elements elementsGroup = document.getElementsByClass("bm");
    for (Element bm : elementsGroup) {
      PlateGroup plateGroup = new PlateGroup();

      Element bm_h = bm.getElementsByClass("bm_h").first();
      String title = bm_h.text();
      plateGroup.setTitle(title);
      List<Plate> plates = new ArrayList<Plate>();
      Elements plateElements = bm.getElementsByClass("bm_c");

      for (Element bm_c : plateElements) {
        Plate plate = new Plate();
        Element a = bm_c.getElementsByTag("a").first();
        String plateTitle = a.text();
        String url = a.absUrl("href");
        Elements count = bm_c.getElementsByClass("xg1");
        String xg1 = null;
        if (count.size() != 0) {
          xg1 = count.first().text();
        } else {
          xg1 = "(0)";
        }
        plate.setTitle(plateTitle);
        plate.setUrl(url);
        plate.setXg1(xg1);

        plates.add(plate);
      }

      plateGroup.setPlates(plates);
      groups.add(plateGroup);
    }

    return groups;
  }
Beispiel #2
0
  /**
   * 解析帖子列表
   *
   * @param Content
   */
  public static ThreadListWrap parseThreadList(String content) {
    ThreadListWrap threadWrap = new ThreadListWrap();
    List<Thread> threads = new ArrayList<Thread>();
    List<Plate> plates = null;

    Document document = Jsoup.parse(content);
    document.setBaseUri(Constants.BASE_URL);
    Elements elementsGroup = document.getElementsByClass("bm_c");
    for (Element bmc : elementsGroup) {
      try {
        Thread thread = new Thread();
        Elements xg1 = bmc.getElementsByClass("xg1");
        String timeAndCount = xg1.first().ownText();
        Elements as = bmc.getElementsByTag("a");
        Element a1 = as.first();
        String url = a1.absUrl("href");
        String title = a1.text();
        String style = a1.attr("style");
        if (!TextUtils.isEmpty(style)) {
          int s = style.indexOf("color");
          if (s != -1) {
            s += 5;
            s = style.indexOf(":", s);
            int e = style.indexOf(";", s);
            String color = style.substring(s + 1, e);
            try {
              thread.setTitleColor(Color.parseColor(color.trim()));
            } catch (Exception e2) {
              e2.printStackTrace();
            }
          }
        }

        Elements imgElements = bmc.getElementsByTag("img");
        if (imgElements != null && imgElements.size() != 0) {
          String src = imgElements.first().absUrl("src");
          thread.setImgSrc(src);
        }

        Element a2 = as.get(1);
        String by = a2.text();

        thread.setBy(by);
        thread.setTitle(title);
        thread.setUrl(url);
        thread.setTimeAndCount(timeAndCount);

        threads.add(thread);
      } catch (Exception e) { // 当有子版块时
        if (plates == null) {
          plates = new ArrayList<Plate>();
        }
        Element child = bmc.child(0);
        Plate plate = new Plate();
        String title = child.ownText();
        String url = child.absUrl("href");
        plate.setTitle(title);
        plate.setUrl(url);
        plate.setSubPlate(true);
        plates.add(plate);

        LogMessage.i(TAG, plate);
      }
    }
    threadWrap.setThreads(threads);
    threadWrap.setPlates(plates);
    return threadWrap;
  }