package com.eric.pickupjoke.activity;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements; import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.SimpleAdapter;
import android.widget.Toast; import com.eric.pickupjoke.conn.IsConnection;
import com.eric.pickupjoke.listview.MyListView;
import com.eric.pickupjoke.listview.MyListView.OnMoreListener;
import com.eric.pickupjoke.listview.MyListView.OnRefreshListener; public class AdultJokeActivity extends Activity {
MyListView articleListView;
List<HashMap<String, String>> articleList;
SimpleAdapter adapter;
OnRefreshListener mOnRefreshListener;
OnMoreListener mOnMoreListener;
static int index = 2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adult_joke);
init();
} void init() {
getWindow().setBackgroundDrawable(new ColorDrawable(-1));
articleListView = (MyListView) findViewById(R.id.article_list_view);
articleList = new ArrayList<HashMap<String, String>>();
InputStream is;
try {
is = this.getResources().getAssets().open("source_html.html");
Document doc = Jsoup.parse(is, "GBK", "http://www.haha365.com");
Log.i("AsyncTask in AdultJokeActivity", "5");
Elements twoArticleElements = doc
.select("div.content>div.left>div.r_c>div.cat_llb");
Log.i("AsyncTask in AdultJokeActivity", "6");
if (twoArticleElements != null && twoArticleElements.size() > 0) {
for (Element twoArticleElement : twoArticleElements) {
Log.i("AsyncTask in AdultJokeActivity", "7");
List<TextNode> textList = twoArticleElement.textNodes();
Map<String, String> map1 = new HashMap<String, String>();
Map<String, String> map2 = new HashMap<String, String>();
Log.i("AsyncTask in AdultJokeActivity", "8");
map1.put("time", textList.get(0).text());
map1.put("title", twoArticleElement.child(1).text());
map1.put("content", twoArticleElement.child(3).text());
map2.put("time", textList.get(4).text());
map2.put("title", twoArticleElement.child(6).text());
map2.put("content", twoArticleElement.child(8).text());
Log.i("AsyncTask in AdultJokeActivity", "9");
articleList.add((HashMap<String, String>) map1);
articleList.add((HashMap<String, String>) map2);
Log.i("AsyncTask in AdultJokeActivity", "10");
}
}
} catch (IOException e) {
e.printStackTrace();
}
Log.i("init() in AdultJokeActivity", "1");
adapter = new SimpleAdapter(this, articleList,
R.layout.article_list_item, new String[] { "title", "content",
"time" }, new int[] { R.id.title, R.id.content,
R.id.time });
articleListView.setAdapter(adapter);
Log.i("init() in AdultJokeActivity", "2"); mOnRefreshListener = new OnRefreshListener() {
@Override
public void onRefresh() {
Log.i("onRefresh in AdultJokeActivity", "1");
if (new IsConnection(AdultJokeActivity.this).isConnected()) {
new LoadArticleTask()
.execute("http://www.haha365.com/Adult_joke/"); }
}
};
mOnMoreListener = new OnMoreListener() {
@Override
public void onMore() {
Log.i("onMore in AdultJokeActivity", "1");
if (new IsConnection(AdultJokeActivity.this).isConnected()
&& index <= 18) {
new LoadArticleTask()
.execute("http://www.haha365.com/Adult_joke/index_"
+ String.valueOf(index) + ".htm");
index++;
} }
};
articleListView.setOnRefreshListener(mOnRefreshListener);
articleListView.setOnMoreListener(mOnMoreListener);
} private class LoadArticleTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
Log.i("AsyncTask in AdultJokeActivity", "1");
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls[0]);
HttpResponse httpResponse = httpClient.execute(httpGet);
Log.i("AsyncTask in AdultJokeActivity", "2");
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
Log.i("AsyncTask in AdultJokeActivity", "3");
String html = EntityUtils.toString(
httpResponse.getEntity(), "GBK");
// System.out.println(html);
Log.i("AsyncTask in AdultJokeActivity", "4");
return html;
} else {
Toast.makeText(AdultJokeActivity.this, "获取HTML文档失败",
Toast.LENGTH_SHORT).show();
return null;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
} @Override
protected void onPostExecute(String html) {
Document doc = Jsoup.parse(html);
Log.i("AsyncTask in AdultJokeActivity", "5");
Elements twoArticleElements = doc
.select("div.content>div.left>div.r_c>div.cat_llb");
Log.i("AsyncTask in AdultJokeActivity", "6");
if (twoArticleElements != null && twoArticleElements.size() > 0) {
for (Element twoArticleElement : twoArticleElements) {
Log.i("AsyncTask in AdultJokeActivity", "7");
List<TextNode> textList = twoArticleElement.textNodes();
Map<String, String> map1 = new HashMap<String, String>();
Map<String, String> map2 = new HashMap<String, String>();
Log.i("AsyncTask in AdultJokeActivity", "8");
map1.put("time", textList.get(0).text());
map1.put("title", twoArticleElement.child(1).text());
map1.put("content", twoArticleElement.child(3).text());
map2.put("time", textList.get(4).text());
map2.put("title", twoArticleElement.child(6).text());
map2.put("content", twoArticleElement.child(8).text());
Log.i("AsyncTask in AdultJokeActivity", "9");
articleList.add((HashMap<String, String>) map1);
articleList.add((HashMap<String, String>) map2);
Log.i("AsyncTask in AdultJokeActivity", "10");
}
} else {
Log.i("AsyncTask in AdultJokeActivity", "8");
}
adapter.notifyDataSetChanged();
articleListView.onRefreshComplete("更新时间:" + getCalendarString());
articleListView.onMoreComplete();
Log.i("AsyncTask in AdultJokeActivity", "11");
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.adult_joke, menu);
return true;
} public String getCalendarString() {
Calendar c = Calendar.getInstance(Locale.CHINA);
int mHour = c.get(Calendar.HOUR_OF_DAY);
int mMinter = c.get(Calendar.MINUTE);
return String.valueOf(mHour) + ":" + String.valueOf(mMinter);
} }

  

Jsoup解析html终于成功了!!!的更多相关文章

  1. 一步步教你为网站开发Android客户端---HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新ListView

    本文面向Android初级开发者,有一定的Java和Android知识即可. 文章覆盖知识点:HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新List ...

  2. Android利用Jsoup解析html 开发网站客户端小记。

    这些天业余时间比较多,闲来无事,想起了以前看过开发任意网站客户端的一篇文章,就是利用jsoup解析网站网页,通过标签获取想要的内容.好了废话不多说,用到的工具为 jsoup-1.7.2.jar包,具体 ...

  3. [java] jsoup 解析网页获取省市区域信息

    到国家统计局抓取数据, 到该class下解析数据 /** * jsoup解析网页 * @author xwolf * @date 2016-12-13 18:11 * @since V1.0.0 */ ...

  4. jsoup解析HTML及简单实例

    jsoup 中文参考文献    http://www.open-open.com/jsoup/ 本文将利用jsoup,简单实现网络抓取的功能,并给出一个小实例,该实例效果为:获取作者本人在博客园写的所 ...

  5. jsoup解析HTML

    Connection conn = Jsoup.connect(String url); conn.data("txtBill", key);// 设置关键字查询字段 Docume ...

  6. Android开发探秘之三:利用jsoup解析HTML页面

    这节主要是讲解jsoup解析HTML页面.由于在android开发过程中,不可避免的涉及到web页面的抓取,解析,展示等等,所以,在这里我主要展示下利用jsoup jar包来抓取cnbeta.com网 ...

  7. 终于成功仿了一次Kalman滤波器

    终于成功仿了一次Kalman滤波器 首先是测试了从网上down的一段代码 % KALMANF - updates a system state vector estimate based upon a ...

  8. Jsoup 解析 HTML

    Jsoup 文档 方法 要取得一个属性的值,可以使用Node.attr(String key) 方法 对于一个元素中的文本,可以使用Element.text()方法 对于要取得元素或属性中的HTML内 ...

  9. Jsoup解析HTML、加载文档等实例

    一.引入jsoup的jar包:http://jsoup.org/download 补充:http://jsoup.org/apidocs/   Jsoup API    可以了解更详细的内容 二.Js ...

随机推荐

  1. 使用VS Code开发ASP.NET 5 应用程序

    本文简要地翻译了 https://code.visualstudio.com/Docs/runtimes/ASPnet5 并结合我的实践做了一些说明. 准备工作 1.安装VS Code  https: ...

  2. 窥探Swift之别样的枚举类型

    想必写过程序的童鞋对枚举类型并不陌生吧,使用枚举类型的好处是多多的,在这儿就不做过多的赘述了.Fundation框架和UIKit中的枚举更是数不胜数,枚举可以使你的代码更易阅读并且可以提高可维护性.在 ...

  3. 真正解决问题:maven eclipse tomcat java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener--转

    原文地址:http://www.cnblogs.com/amosli/p/4067665.html 在使用eclipse进行整合springmvc时经常会碰到这样的异常: java.lang.Clas ...

  4. iOS_UIImage的方向(imageOrientation)

    Demo下载地址 一.重现问题: 拍照获得的图片,由于尺寸不对,需要拆切.但是有的图片拆切出来,方向就乱了.查找了一些质量才知道.图片还有个方向属性! imageOrientation. 二.一个图片 ...

  5. SQL 性能优化-查询优化(like查询)

    废话不说,上代码 SET STATISTICS IO ON SELECT * FROM dbo.T_AssNews WHERE Content LIKE '%会%' 花费时间 执行计划 一个百分号的代 ...

  6. httpModules与Http模块

    httpModules是往当前应用程序添加HttpModule(http模块)的标签.配置节如下 <httpModules> <add name="ModuleName&q ...

  7. 【Win10开发】关于AutoSuggestBox

    其实看名字我们就知道,这个控件可以提供一些建议文本.我们在做搜索框时可以做一些文本来让用户选择. 这个控件有两个关键的事件QuerySubmitted和SuggestionChosen事件,当下拉列表 ...

  8. jQuery+HTML5弹出创意搜索框层

    效果体验:http://hovertree.com/texiao/jquery/26/ 本效果适用于移动设备,可以使用手机等浏览效果. 代码下载:http://hovertree.com/h/bjaf ...

  9. B/S工作原理

    B/S疑问 先对比C/S,在C/S中我们开发时怎么做的,是不是这样:拖控件,写方法,所有的功能基本就是这样,就像我们的机房收费系统,C/S学习完之后,我们开始B/S学习,这里我们接触的是ASP.NET ...

  10. ArcGIS10.2 应用服务器搭建

    操作系统:Windows Server2012R2 DataCenter 软件环境:ArcGIS Desktop10.2,ArcSDE10.2,ArcGIS Server10.2,win64_11gR ...