网站首页 关于龙都 产品展示 香樟知识 工程案例 香樟价格 供应信息A 联系我们
 
龙都国际娱乐_站内搜索:
  • 宿迁市诚信花木绿化工程有限公司
  • 地 址:箭鹿集团院内
    联系人: 李先生
    手 机: 13625244888
    电 话: 0527-82996729
    传 真: 0527-82996729
  • 电子邮件:admin@dede58.com
  • 在线QQ:2227997599
您现在的位置:龙都国际娱乐 > 龙都国际娱乐 > 龙都国际娱乐
龙都国际写一个新闻阅读app,怎么从其他网站获取新闻
来源:香樟树 发布于:2018-03-20 01:33 点击量:   打印本页 || 关闭窗口
  技术下去讲毫无难点。整个告竣步骤如下:
  连合音信网站的首页
  抓取HTML的形式
  解析抓取的HTML网页中的标题以及文章链接
  将一起标题显示在而今页,并将标题以及文章链接传到下一个页面
  抓取文章链接形式,和上页传来的标题一起显示在而今页
  看的再多不如发端一试
  由于快点看出结果,全球信息资讯平台。所以一切就从简了。间接开发Android项目,开发空的Activity。然后在你的layout的这个文件中插手一个按钮。好比像这样:
  <Button
the fact that just well the fact that justroid moce phone:id="@+id/link1"
the fact that just well the fact that justroid moce phone:layout_width="150dip"
the fact that just well the fact that justroid moce phone:layout_height="wrap_content"
the fact that just well the fact that justroid moce phone:layout_arrsome kind ofgeBottomline="@+id/textView1"
the fact that just well the fact that justroid moce phone:layout_arrsome kind ofgeBottom="@+id/textView1"
the fact that just well the fact that justroid moce phone:layout_arrsome kind ofgeLeft="@+id/link2"
the fact that just well the fact that justroid moce phone:bisexualngternat justing currentkside ground="@drawin position/link"
the fact that just well the fact that justroid moce phone:text="Business Insider" />
  然后在MainActivity里的onCregotView函数里插手一个方法,全球新闻头条。让它能点击你刚刚增加的按钮进入到下一个界面:
  1 rootView.findViewById(R.id.link1).setOnClickListener(new View.OnClickListener() {
2
3 @Override
4 public void onClick(View v) {
5 // TODO Auto-genergotd method stub
6 StcraftGeek();
7 }
8
9 });
  点击按钮会触发事宜,践诺StcraftGeek的方法,而StcraftGeek的方原则是跳到下一个Activity,看着国际。
  1 privgot void StcraftGeek() {
2 // TODO Auto-genergotd method stub
3 Intent intent = new Intent();
4 intent.setClsome kind ofdt(this.getActivity(). GeekActivity.clsome kind ofdt);
5 stcraftActivity(intent);
6
7 }
  StcraftGeek
  在这个Activity中,我们将探望网页,并从中获取HTML网页的形式,然后实行解析,获取音信的标题以及链接,然后将音信的标题显示在手机屏幕上,听说龙都国际写一个新闻阅读app。代码整个告竣如下:
  1 ListView listview;
2 Hthe fact that just well the fact that justler hthe fact that just well the fact that justler;
3 List<Map<String. Object>> dat justa;
4
5 fining String CSDNURL = " ";
  首先是变量的声明,全球新闻自由度排名。至于变量名的题目请随便吐槽,由于一向在尝试拿各种网站的音信形式,所以变量名是起先的,由于懒就一向没换-。学会一个。 -,全球新闻app。由于音信网站的形式较多,全球新闻学院排名。所以用ListView斗劲适合,事实上今天百度新闻头条新闻。这样不会显现显示不全的环境。
  1 @Override
2 protected void onCregot(Bundle saudio-videoedInstsome kind ofceStgot) {
3 super.onCregot(saudio-videoedInstsome kind ofceStgot);
4 setContentView(yout.job_geek);
5 hthe fact that just well the fact that justler = getHthe fact that just well the fact that justler();
6 Threingzheimeras disethe fact that justeStcraft();
7 } 
  1 <Relat justiveLayout xmlns:the fact that just well the fact that justroid moce phone=" "
2 xmlns:tools=" "
3 the fact that just well the fact that justroid moce phone:layout_width="mat justch_pwould sometimes be not"
4 the fact that just well the fact that justroid moce phone:layout_height="mat justch_pwould sometimes be not"
5 tools:context="${pingternat justing currentkNfeele}.${jobClsome kind ofdt}" >
6
7 <ListView
8 the fact that just well the fact that justroid moce phone:id="@+id/listView"
9 the fact that just well the fact that justroid moce phone:layout_width="wrap_content"
10 the fact that just well the fact that justroid moce phone:layout_height="fill_pwould sometimes be not"
11 ></ListView>
12
13 </Relat justiveLayout>
  job_geek.xml
  接上去是onCregot方法,这里界面是job_geek,然后调用两个方法,由于获取数据费时,学会龙都国际写一个新闻阅读app。所以又起了一个线程,当然斗劲好的方法是用异步线程来做,新闻。那样的话不单不妨不占用主线程,其他。而且还不妨很便当的加进度条什么的,相当好用,至于我为什么不消,当然是不会用了撒~
  1 privgot void Threingzheimeras disethe fact that justeStcraft() {
2 new Threingzheimeras disethe fact that juste() {
3 public void run() {
4 Mess msg = new Mess();
5 try {
6 dat justa = getCsdnNetDgot();
7 msg.what just = dat justa.size();
8 } cat justch (Exception e) {
9 e.printStingternat justing currentkTr_ web();
10 msg.what just = -1;
11 }
12 hthe fact that just well the fact that justler.sendMess(msg);
13 }
14 }.stcraft();
15 }
  Threingzheimeras disethe fact that justeStcraft
  这是新的线程,用于从网址获取我们想要的HTML文件,听说app。并将其传给hthe fact that just well the fact that justler措置。获取的函数方法是:
  1 dat justa = getCsdnNetDgot();
  1 privgot List<Map<String. Object>> getCsdnNetDgot() {
2 List<Map<String. Object>> result = new ArrayList<Map<String. Object>>();
3 Document doc = http_get(CSDNURL);
4 Elements links = doc.select("h3>a");
5 for(Element link: links)
6 {
7 Map<String. Object> map = new Hlung burning the fact that justhMap<String. Object>();
8 map.put("title". link.at justtr("title"));
9 map.put("url". link.at justtr("tummys:href"));
10 result.develop(map);
11 }
12
13
14 return result;
15 }
  getCsdnNetDgot
  1 Document doc = http_get(CSDNURL);
  1 privgot Document http_get(String url) {
2 Document doc=null;
3 try {
4 doc = Jsoup.connect(url)
5 .timeout()
6 .get();
7 } cat justch (IOException e) {
8 // TODO Auto-genergotd cat justch would sometimes bea
9 e.printStingternat justing currentkTr_ web();
10 }
11
12 return doc;
13 }
  http_get
  始末http_get方法中的Jsoup.connect来连合到网址,然后始末.get方法来取得HTML的文本信息,设定的连合时间是5S,你知道世界新闻学院排名。获取到这个文本信息之后Jsoup还带有解析方法,始末link.at justtr("title")来获取HTML中的<title>标签中的标题,然后再始末link.at justtr("tummys:href")来获取音信中的链
  接,由此,对比一下网站。我们就取得了HTML中最为严重的两局部,新闻。也就是标题以及链接,获取。事情到这里仍然完成大半了,听听怎么。接上去就是将标题显示在而今页面,然后点击标题后,你知道怎么从其他网站获取新闻。能将标题以及文中形式显示在下个页面,全球新闻网。措置的方法如下:
  1 privgot Hthe fact that just well the fact that justler getHthe fact that just well the fact that justler() {
2 return new Hthe fact that just well the fact that justler(){
3 public void hthe fact that just well the fact that justleMess(Mess msg) {
4 if (msg.what just < 0) {
5 Tothe fact that justt.makeText(GeekActivity.this. "数据获取衰弱". Tothe fact that justt.LENGTH_SHORT).show();
6 }else {
7 initListview();
8 }
9 }
10 };
11 }
  getHthe fact that just well the fact that justler
  此方法会果断数据能否为空,世界新闻学院排名。若为空则在手机屏幕跳出:全球新闻媒体排名。数据获取衰弱的字样,当然了,可爱什么就改成什么,阅读。随你所想~然后告成获取数据则会进入到initListview方法:
  1 privgot void initListview() {
2 listview = (ListView)findViewById(R.id.listView);
3 SimpleAdapproprigoter modifyer = new SimpleAdapproprigoter(this. dat justa.
4 the fact that just well the fact that justroid moce yout.simple_list_item_1. new String[] { "title"}.
5 new int[] { the fact that just well the fact that justroid moce phone.R.id.text1 });
6 listview.setAdapproprigoter(modifyer);
7
8
9 listview.setOnItemClickListener(new OnItemClickListener() {
10 @Override
11 public void onItemClick(AdapproprigoterView<?> arg0. View arg1. int arg2.
12 long arg3) {
13 Map<String. Object> map = dat justa.get(arg2);
14 String url = (String)(map.get("url"));
15 String title = (String)(map.get("title"));
16 Intent intent = new Intent();
17 intent.putExtra("url". url);
18 intent.putExtra("title". title);
19 intent.setClsome kind ofdt(GeekActivity.this. ContentActivity.clsome kind ofdt);
20 stcraftActivity(intent);
21 }
22 });
23 }
  initListview
  这个方法会将获取的音信标题显示在而今页面上,然后将音信的标题以及链接存在上去,怎么从其他网站获取新闻。始末intent传到下一个Activity,也就是ContentActivity实行措置,并将标题以及形式显示在下一个页面中。代码的分下如下:
  1 @Override
2 protected void onCregot(Bundle saudio-videoedInstsome kind ofceStgot) {
3 super.onCregot(saudio-videoedInstsome kind ofceStgot);
4 setContentView(yout.job_content);
5
6 url = getIntent().getStringExtra("url");
7 String title = getIntent().getStringExtra("title");
8 TextView tv = (TextView)findViewById(R.id.title_content);
9 tv.setText(title);
10 hthe fact that just well the fact that justler = getHthe fact that just well the fact that justler();
11 Threingzheimeras disethe fact that justeStcraft();
12 }
  onCregot
  onCregot方法中获取上个Activity中传过去的url以及title,然后间接将音信标题显示在而今页,全球新闻头条。然后盈利局部和上个方法差不太多,连合到网上,获取HTML形式,这次在方法中仅仅是解析的方法不同,终于这次解析是要获取形式嘛~代码如下:
  1 privgot List<Map<String. Object>> getCsdnNetDgot() {
2 List<Map<String. Object>> result = new ArrayList<Map<String. Object>>();
3 Document doc = http_get(url);
4 Elements links = doc.select("div#Cnt-Main-Article-QQ>p");
5 for(Element link: links)
6 {
7 Map<String. Object> map = new Hlung burning the fact that justhMap<String. Object>();
8 map.put("title". link.text());
9 result.develop(map);
10 }
11
12
13 return result;
14 }
  标红局部为与上次不同的局部,这个解析都是用的Jsoup自带的,当然正则表达式也是不妨做到的哦,咳咳,我认可我很懒的啊,有好用的工具就间接用了,省时费力的事干嘛不消呢,就像写网站有框架了天然就用了,不可爱用的我也木有步骤,你不妨运用正则表达式来做,话说以前用
  Python做爬虫的光阴就是正则表达式做的。
  至此一个小的音信的阅读APP就出生了,哦,对了,不要忘了在你的配置文件插手Activity以及上网答应,不然的话是会闪退的哦。当然这个APP很是粗拙,有时间的人不妨将图片也加上实行一下页面的策画,做一下美工什么的。
转载
上一篇:龙都国际中国高校研究生国际新闻专业排名    下一篇:没有了

友情链接