Ejemplo n.º 1
0
 // 进行json解析
 private void analyzeJson(String json) {
   if (!TextUtils.isEmpty(json)) {
     String[] cir_arr_one = json.split("\\(");
     String cir_one = cir_arr_one[1];
     String[] cir_arr_two = cir_one.split("\\)");
     String cir_two = cir_arr_two[0];
     if ("null".equals(cir_two)) {
       // 标明 已经全部加载完
       // 证明已经到了最后一页
       isLastPage = true;
       ToastAlone.show(R.string.load_all_data);
       Log.i("cxm", "the last one");
       return;
     } else {
       Gson gson = new Gson();
       List<StoryBean> storyBeans =
           gson.fromJson(cir_two, new TypeToken<List<StoryBean>>() {}.getType());
       if (storyBeans != null) {
         if (mStartNum == 20) {
           mStoryAdapter.setData(storyBeans);
         } else {
           mStoryAdapter.addData(storyBeans);
         }
       }
     }
   }
 }
Ejemplo n.º 2
0
    @Override
    protected void onPostExecute(Document document) {
      super.onPostExecute(document);
      story_pull_list.onRefreshComplete();
      File file = new File(Environment.getExternalStorageDirectory() + "/Latest_qbaobei.txt");
      try {
        if (!file.exists()) {
          file.createNewFile();
        }
        String str = document.toString();
        FileWriter writer = new FileWriter(file.getAbsolutePath());
        BufferedWriter bufferedWriter = new BufferedWriter(writer);
        bufferedWriter.write(str);
        bufferedWriter.close();
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (document == null) {
        ToastAlone.show(R.string.load_fail_hint);
        return;
      }
      Element page = document.select("div.page").first();
      if (page != null) {
        Elements children = page.children();
        if (children.size() == 1 && "prev".equals(children.first().attr("class"))) {
          // 证明已经到了最后一页
          isLastPage = true;
          ToastAlone.show(R.string.load_all_data);
          Log.i("cxm", "the last one");
          return;
        }
      }
      progressbar.setVisibility(View.GONE);
      // 使用新的爬虫规则
      Elements div_elements = document.select("div.news-list-ul");
      if (div_elements != null) {
        Element div_fir = div_elements.first();
        if (div_fir != null) {
          Elements div_children = div_fir.children();
          if (div_children != null) {
            ArrayList<StoryBean> storyBeans = new ArrayList<StoryBean>();
            for (Element element : div_children) {
              StoryBean bean = new StoryBean();
              Element href_elem = element.select("a[href]").first();
              String href_str = href_elem.attr("href");
              Element img_elem = element.select("img[src]").first();
              String img_str = Constans.defualt_pic;
              if (img_elem != null) {
                img_str = img_elem.attr("src");
              }
              Element tit_element = element.select("p.tit").first();
              String tit_str = tit_element.text();
              LogUtil.v(tit_str + "---" + href_str + "---" + img_str);
              bean.setTitle(tit_str);
              bean.setImg(img_str);
              bean.setUrl(href_str);
              storyBeans.add(bean);
            }
            if (mLatestStart == 1) {
              mStoryAdapter.setData(storyBeans);
            } else {
              mStoryAdapter.addData(storyBeans);
            }
          }
        }
      } else {
        // 立马启动第二种解析方式
        Elements ul_elements = document.select("ul.index-ul");
        if (ul_elements != null) {
          Element ul_fir = ul_elements.first();
          Elements ul_children = ul_fir.children();
          if (ul_children != null) {
            ArrayList<StoryBean> storyBeans = new ArrayList<StoryBean>();
            for (Element child : ul_children) {
              StoryBean bean = new StoryBean();
              Element href_elem = child.select("a[href]").first();
              Element img_elem = child.select("img[src]").first();
              String title = href_elem.text();
              String content_url = href_elem.attr("href");
              String img_url = Constans.defualt_pic;
              if (img_elem != null) {
                img_url = img_elem.attr("src");
              }
              bean.setTitle(title);
              bean.setUrl(content_url);
              bean.setImg(img_url);
              storyBeans.add(bean);
            }
            if (mLatestStart == 1) {
              mStoryAdapter.setData(storyBeans);
            } else {
              mStoryAdapter.addData(storyBeans);
            }
          }
        }
      }

      /*Elements elements = document.select("[class]");
      for (Element element : elements) {
          if (element == null) {
              Log.v("cxm", "null");
          } else {
              String className = element.className();
              if ("index-ul".equals(className)) {

                  Elements elements1 = element.select("li");
                  Log.v("cxm", "size=" + elements1.size());
                  ArrayList<StoryBean> storyBeans = new ArrayList<StoryBean>();
                  for (Element child : elements1) {
                      StoryBean bean = new StoryBean();
                      Element href = child.select("[href]").first();
                      String name = href.text();
                      Element img = child.select("img[src]").first();
                      Log.w("cxm", "href=" + href.attr("href") + " ~~ name=" + name + "" +
                              " ~~ img=");
                      bean.setTitle(name);
                      bean.setmContentUrl(href.attr("href"));
                      if (null == img) {
                          Log.e("cxm", "img = null");
                          bean.setPicUrl("");
                      } else {
                          bean.setPicUrl(img.attr("src"));
                      }
                      storyBeans.add(bean);
                  }
                  if (mLatestStart == 1) {
                      mStoryAdapter.setData(storyBeans);
                  } else {
                      mStoryAdapter.addData(storyBeans);
                  }
              }
          }
      }*/
    }