use of org.mazhuang.guanggoo.data.entity.Topic in project guanggoo-android by mzlogin.
the class GetTopicListTask method run.
@Override
public void run() {
List<Topic> topics = new ArrayList<>();
boolean succeed = false;
boolean hasMore = false;
try {
Document doc = get(mUrl);
Elements elements = doc.select("div.topic-item");
for (Element element : elements) {
Topic topic = createTopicFromElement(element);
topics.add(topic);
}
succeed = true;
Elements paginationElements = doc.select("ul.pagination");
if (!paginationElements.isEmpty()) {
Elements disabledElements = paginationElements.select("li.disabled");
if (disabledElements.isEmpty()) {
hasMore = true;
} else if (disabledElements.last() != null) {
Elements disableLinkElements = disabledElements.last().select("a");
if (!ConstantUtil.NEXT_PAGE.equals(disableLinkElements.text())) {
hasMore = true;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (succeed) {
TopicList topicList = new TopicList();
topicList.setTopics(topics);
topicList.setHasMore(hasMore);
successOnUI(topicList);
} else {
failedOnUI("获取主题列表失败");
}
}
use of org.mazhuang.guanggoo.data.entity.Topic in project guanggoo-android by mzlogin.
the class GetTopicListTask method createTopicFromElement.
public static Topic createTopicFromElement(Element element) {
Topic topic = new Topic();
Element titleElement = element.select("h3.title").select("a").first();
if (titleElement != null) {
// 主题列表页
topic.setTitle(titleElement.text());
} else {
// 主题详情页
titleElement = element.select("h3.title").first();
if (titleElement != null) {
topic.setTitle(titleElement.text());
}
}
topic.setUrl(titleElement.absUrl("href"));
topic.setAvatar(element.select("img.avatar").attr("src"));
Element metaElement = element.select("div.meta").first();
Meta meta = createMetaFromElement(metaElement);
topic.setMeta(meta);
Elements countElements = element.select("div.count");
if (!countElements.isEmpty()) {
topic.setCount(Integer.valueOf(countElements.first().select("a").first().text()));
}
return topic;
}
use of org.mazhuang.guanggoo.data.entity.Topic in project guanggoo-android by mzlogin.
the class GetTopicDetailTask method run.
@Override
public void run() {
Document doc;
try {
doc = get(mUrl);
} catch (IOException e) {
e.printStackTrace();
failedOnUI(e.getMessage());
return;
}
Elements topicDetailElements = doc.select("div.topic-detail");
if (topicDetailElements.isEmpty()) {
failedOnUI("找不到主题详情");
return;
}
Elements elements = topicDetailElements.select("div.ui-header");
if (elements.isEmpty()) {
failedOnUI("找不到主题元信息");
return;
}
TopicDetail topicDetail = new TopicDetail();
Topic topic = GetTopicListTask.createTopicFromElement(elements.first());
topicDetail.setTopic(topic);
// 解析收藏
Favorite favorite = new Favorite();
Elements favouriteElement = doc.select(".J_topicFavorite");
if (favouriteElement != null) {
String dataType = favouriteElement.attr("data-type");
favorite.setFavorite(!Favorite.TYPE_NOT_FAVORITE.equals(dataType));
}
topicDetail.setFavorite(favorite);
elements = topicDetailElements.select("div.ui-content");
if (elements.isEmpty()) {
failedOnUI("找不到主题内容");
return;
}
topicDetail.setContent(elements.first().outerHtml());
Elements commentsElements = doc.select("div.topic-reply");
Map<Integer, Comment> comments = GetCommentsTask.getCommentsFromElements(commentsElements);
topicDetail.setComments(comments);
successOnUI(topicDetail);
}
use of org.mazhuang.guanggoo.data.entity.Topic in project guanggoo-android by mzlogin.
the class GetReplyListTask method run.
@Override
public void run() {
List<Reply> replies = new ArrayList<>();
boolean succeed = false;
boolean hasMore = false;
try {
Document doc = get(mUrl);
Elements elements = doc.select("div.reply-item");
for (Element element : elements) {
Reply reply = new Reply();
Elements topicElements = element.select("span.title a");
if (topicElements.isEmpty()) {
continue;
}
Topic topic = new Topic();
topic.setTitle(topicElements.first().text());
topic.setUrl(topicElements.first().absUrl("href"));
reply.setTopic(topic);
Elements contentElements = element.select("div.content");
if (contentElements.isEmpty()) {
continue;
}
reply.setContent(contentElements.outerHtml());
replies.add(reply);
}
succeed = true;
Elements paginationElements = doc.select("ul.pagination");
if (!paginationElements.isEmpty()) {
Elements disabledElements = paginationElements.select("li.disabled");
if (disabledElements.isEmpty()) {
hasMore = true;
} else if (disabledElements.last() != null) {
Elements disableLinkElements = disabledElements.last().select("a");
if (!ConstantUtil.NEXT_PAGE.equals(disableLinkElements.text())) {
hasMore = true;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (succeed) {
ReplyList replyList = new ReplyList();
replyList.setReplies(replies);
replyList.setHasMore(hasMore);
successOnUI(replyList);
} else {
failedOnUI("获取回复列表失败");
}
}
Aggregations