package com.martin.ionichinabystudio.model.impl; import android.content.Context; import com.android.volley.Response; import com.android.volley.VolleyError; import com.google.gson.Gson; import com.martin.ionichinabystudio.config.Constant; import com.martin.ionichinabystudio.config.HttpConfig; import com.martin.ionichinabystudio.model.entity.Topic; import com.martin.ionichinabystudio.model.entity.TopicDetailsEntity; import com.martin.ionichinabystudio.model.entity.TopicListResult; import com.martin.ionichinabystudio.model.inter.TopicModel; import com.martin.ionichinabystudio.presenter.inter.OnResultListener; import com.martin.simpledevelop.utils.cache.ACache; import com.martin.simpledevelop.utils.file.SaFileUtils; import com.martin.simpledevelop.utils.http.volley.VolleyRequest; import com.martin.simpledevelop.utils.log.SaLogUtils; import com.martin.simpledevelop.utils.security.SaMD5Utils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.File; import java.util.ArrayList; /** * @Description 话题的Model实现 * @File TopicModelImpl.java * @Package com.martin.ionichinabystudio.model.data * @Date 2015/7/214:52 * @Author Donghongyu 1358506549@qq.com * @Version v1.0.0 */ public class TopicModelImpl implements TopicModel { /** * 数据缓存工具 */ private ACache mCache = null; //将json数据转换成对象 private Gson gson; public TopicModelImpl(Context context) { // 构建指定文件夹的缓存 mCache = ACache .get(new File(SaFileUtils.getCacheDownloadDir(context))); gson = new Gson(); } @Override public void getTopics(String tabType, int currentPage, final OnResultListener listener) { //进行网络访问链接的拼装操作 final String getTopicsUrl = HttpConfig.API_GET_TOPICS + "?tab=" + tabType + "&page=" + currentPage + "&limit=10&mdrender=true"; /*数据层操作*/ VolleyRequest.newInstance().newGsonRequest(getTopicsUrl, TopicListResult.class, new Response.Listener<TopicListResult>() { @Override public void onResponse(TopicListResult result) { if (result != null) { // 进行Http响应数据缓存,将请求连接加密作为key mCache.put(SaMD5Utils.encode(getTopicsUrl), gson.toJson(result)); //回调成功的监听 listener.onSuccess(result); } else { listener.onError(); //从缓存中获取到数据 String jsonReslut = mCache.getAsString(SaMD5Utils.encode(getTopicsUrl)); if (jsonReslut != null) { result = gson.fromJson(jsonReslut, TopicListResult.class); listener.onSuccess(result); } } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { listener.onError(); TopicListResult result; //从缓存中获取到数据 String jsonReslut = mCache.getAsString(SaMD5Utils.encode(getTopicsUrl)); if (jsonReslut != null) { result = gson.fromJson(jsonReslut, TopicListResult.class); listener.onSuccess(result); } } }); } @Override public void getTopicDetails(Topic topic, OnResultListener listener) { // 创建话题的的详情的解析结果集合 ArrayList<TopicDetailsEntity> mEntities = new ArrayList<TopicDetailsEntity>(); // 标题 TopicDetailsEntity entity = new TopicDetailsEntity(); entity.setTopicTitle(topic.getTitle()); entity.setTopicType(Constant.TOPIC_TYPE_TITLE); mEntities.add(entity); // 创建时间 entity = new TopicDetailsEntity(); entity.setTopicCreateDate(topic.getCreate_at()); entity.setTopicType(Constant.TOPIC_TYPE_CREATE_DATE); mEntities.add(entity); // // 作者 // entity = new TopicDetailsEntity(); // entity.setTopicAuthor(topic.getAuthor()); // entity.setTopicType(Constant.TOPIC_TYPE_AUTHOR); // mEntities.add(entity); // 内容 Document doc = Jsoup.parse(topic.getContent()); Element contentEle = doc.getElementsByClass("markdown-text").get(0); // 获得话题内容中的子元素 Elements childrenEle = contentEle.children(); SaLogUtils.e("childrenEle", childrenEle.size() + ""); // 循环的遍历内容 for (Element child : childrenEle) { Elements imgEles = child.getElementsByTag("img"); // 图片 if (imgEles.size() > 0) { for (Element imgEle : imgEles) { if (imgEle.attr("src").equals("")) continue; entity = new TopicDetailsEntity(); entity.setTopicImgUrl(imgEle.attr("src")); entity.setTopicType(Constant.TOPIC_TYPE_IMG_URL); mEntities.add(entity); } } if (imgEles != null) // 移除图片 imgEles.remove(); if (child.text().equals("")) continue; entity = new TopicDetailsEntity(); entity.setTopicType(Constant.TOPIC_TYPE_CONTENT); try { if (child.children().size() == 1) { Element cc = child.child(0); if (cc.tagName().equals("b")) { entity.setTopicType(Constant.TOPIC_TYPE_CONTENT); } } } catch (IndexOutOfBoundsException e) { e.printStackTrace(); } //获取元素外HTML内容 entity.setTopicContent(child.outerHtml()); mEntities.add(entity); } if (mEntities.size() != 0) { //回调成功的监听 listener.onSuccess(mEntities); } else { //回调成功的监听 listener.onError(); } } }