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); } }