コード例 #1
0
  /**
   * 解析回帖列表
   *
   * @param content
   * @return
   */
  public static List<Post> parsePostList(String content) {
    long s = System.currentTimeMillis();

    List<Post> posts = new ArrayList<Post>();
    Document document = Jsoup.parse(content);
    document.setBaseUri(Constants.BASE_URL);
    Elements elements = document.getElementsByClass("plc");
    for (Element plc : elements) {
      try {
        Post post = new Post();
        // 解析头像
        Element avatar = plc.getElementsByClass("avatar").first();
        post.setAvatarUrl(avatar.child(0).absUrl("src"));

        String authi = plc.getElementsByClass("authi").first().html();
        Element message = plc.getElementsByClass("message").first();
        post.setContent(message.html().trim());
        // // 解析头像
        // // Element avatar = plc.getElementsByClass("avatar").first();
        // Element avatar = plc.child(0);
        // post.setAvatarUrl(avatar.child(0).absUrl("src"));
        //
        // // Element message = plc.getElementsByClass("message").first();
        // Element display = plc.child(1);
        // String authi = display.child(0).html();
        // Element message = display.child(1);
        // post.setContent(message.html().trim());

        try { // 主贴没有replyUrl
          String replyUrl = plc.getElementsByClass("replybtn").first().child(0).absUrl("href");
          post.setReplyUrl(replyUrl);
        } catch (Exception e) {
        }

        Elements img_list = plc.getElementsByClass("img_list");
        if (img_list != null && !img_list.isEmpty()) {
          String imgList = img_list.first().html();
          post.setImgList(imgList);
        } else { // 单张图片附件时
          Elements img_one = plc.getElementsByClass("img_one");
          if (img_one != null && !img_one.isEmpty()) {
            String imgOne = img_one.first().html();
            post.setImgList(imgOne);
          }
        }
        post.setAuthi(authi);

        posts.add(post);
      } catch (Exception e) {
      }
      LogMessage.i("parsePostList", "解析时间:" + (System.currentTimeMillis() - s));
    }
    return posts;
  }
コード例 #2
0
  /**
   * 解析用户信息
   *
   * @param responseBody
   * @return
   */
  public static User parseUserInfo(String responseBody) {
    User user = new User();
    try {
      Document document = Jsoup.parse(responseBody);
      document.setBaseUri(Constants.BASE_URL);
      Element elementUser = document.getElementsByClass("userinfo").first();
      Element elementAvatar = elementUser.getElementsByTag("img").first();
      user.setAvatarUrl(elementAvatar.attr("src"));
      user.setName(elementUser.getElementsByClass("name").first().text());
      user.setInfo(elementUser.getElementsByClass("user_box").html());

      Element btn_exit = document.getElementsByClass("btn_exit").first();

      String url = btn_exit.child(0).attr("href");
      UrlParamsMap map = new UrlParamsMap(url);
      String formHash = map.get("formhash");
      ChhApplication.getInstance().setFormHash(formHash);

      LogMessage.i("formHash", formHash);
    } catch (Exception e) {
      LogMessage.w(TAG + "#parseUserInfo", e);
    }
    return user;
  }
コード例 #3
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;
  }