Android抓取CSDN首页极客头条内容完整实例

时间:2021-05-20

今天,写了个小代码。抓取首页中的极客头条。效果如图:

分享给新手朋友。

要点:

1.使用ApacheHttpClient库实现GET请求。

2.异步请求处理。

3.正则表达式抓取自己需要的数据。

1.使用ApacheHttpClient库实现GET请求。

使用Apache只需简单三步

HttpClient httpClient = new DefaultHttpClient(); //创建一个HttpClient HttpGet httpGet = new HttpGet(“http://pile("title=\"(.*?)\" href=\"(.*?)\".*?363"); Matcher m = p.matcher(csdnString); while (m.find()) { MatchResult mr=m.toMatchResult(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", mr.group(1)); map.put("url", mr.group(2)); result.add(map); } return result; } /** * 处理联网结果,显示在listview * @return */ private Handler getHandler() { return new Handler(){ public void handleMessage(Message msg) { if (msg.what < 0) { Toast.makeText(MainActivity.this, "数据获取失败", Toast.LENGTH_sHORT).show(); } else { initListview(); } } } ; } /** * 在listview里显示数据 * @author Lai Huan * @created 2013-6-20 */ private void initListview() { listview = getListView(); SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_1, new String[] { "title"}, new int[] { android.R.id.text1 }); listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Map<String, Object> map = data.get(arg2); String url = (String)(map.get("url")); Intent intent = new Intent(Intent.ACTION_VIEW); intent .setData(Uri.parse(url)); startActivity(intent); } } ); } /** * get请求URL,失败时尝试三次 * @param url 请求网址 * @return 网页内容的字符串 */ private String http_get(String url) { final int RETRY_TIME = 3; HttpClient httpClient = null; HttpGet httpGet = null; String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); httpGet = new HttpGet(url); HttpResponse response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == 200) { //用utf-8编码转化为字符串 byte[] bResult = EntityUtils.toByteArray(response.getEntity()); if (bResult != null) { responseBody = new String(bResult,"utf-8"); } } break; } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } e.printStackTrace(); } finally { httpClient = null; } } while (time < RETRY_TIME); return responseBody; } private HttpClient getHttpClient() { HttpParams httpParams = new BasicHttpParams(); //设定连接超时和读取超时时间 HttpConnectionParams.setConnectionTimeout(httpParams, 6000); HttpConnectionParams.setSoTimeout(httpParams, 30000); return new DefaultHttpClient(httpParams); }}

总结

以上就是本文关于Android抓取CSDN首页极客头条内容完整实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章