Android利用Jsoup解析实现简单新闻客户端

预备知识:关于Jsoup,这个问题百度就能为你 解决:

Jsoup

先看效果图:

这是 2015/09/24 18:50左右获取来自腾讯新闻的一些新闻
Android利用Jsoup解析实现简单新闻客户端_第1张图片

这是 2015/09/24 18:50左右获取来自凤凰新闻的一些新闻

Android利用Jsoup解析实现简单新闻客户端_第2张图片

请看代码:

这是 获取数据的代码:

package com.example.cywtest1;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter.ViewBinder;

@SuppressLint("NewApi")
public class MyTab1 extends Fragment {
    private View view;
    private ViewPager viewPager;
    private List lists = new ArrayList();  
    private ViewPagerAdapter adapter;  
    private List webUrl;

    private static boolean isInit = false;

    private ListView lv1;
    private ListView lv2;
    private ListView lv3;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private List> listdata1;
    private List> listdata2;
    private List> listdata3;
    private SimpleAdapter simpleadapter1;
    private SimpleAdapter simpleadapter2;
    private SimpleAdapter simpleadapter3;

    private static boolean listdata1isInit = false;
    private static boolean listdata2isInit = false;
    private static boolean listdata3isInit = false;

    public void onCreate(){
        fillWebUrl();
//      lists.add(getActivity().getLayoutInflater().inflate(R.layout.activity_list1, null));
//        lists.add(getActivity().getLayoutInflater().inflate(R.layout.activity_list2, null));  
//        lists.add(getActivity().getLayoutInflater().inflate(R.layout.activity_list3, null));
    }

    public void fillWebUrl(){
        webUrl = new ArrayList();
        webUrl.add("http://www.news.qq.com");
        webUrl.add("http://www.news.qq.com/world_index.shtml");
        webUrl.add("http://news.ifeng.com/society");
    }

    public View onCreateView(LayoutInflater inflater,
             ViewGroup container,  Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.mytab1, container, false);
        if(!isInit)
        {
            lists.add(inflater.inflate(R.layout.activity_list1, null));
            lists.add(inflater.inflate(R.layout.activity_list2, null));  
//          lists.add(inflater.inflate(R.layout.activity_list3, null));

            isInit = true;
        }



        initView(inflater,view);

        return view;
    }

    void initView(LayoutInflater inflater,View view){

        viewPager = (ViewPager) view.findViewById(R.id.viewpager_firstpage_todaytopline);
        adapter=new ViewPagerAdapter(lists);
        viewPager.setAdapter(adapter);

        listdata1 = new ArrayList>();
        lv1 =(ListView) adapter.getPageList().get(0).findViewById(R.id.listview_news_list1);
        btn1 = (Button)adapter.getPageList().get(0).findViewById(R.id.button1_list1);
        simpleadapter1 = new SimpleAdapter(getActivity(), listdata1, R.layout.rlist, new String[] { "title", "info",
            "img" }, new int[] { R.id.title, R.id.info, R.id.img });
        lv1.setAdapter(simpleadapter1);
        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(!listdata1isInit) {
                    **new GetDataTask1().execute();**/*异步任务类获取数据*/
                    listdata1isInit = true;
                }
            }
        });
        lv1.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                // TODO Auto-generated method stub

                HashMap map = (HashMap)lv1.getItemAtPosition(position);
                String url = map.get("url");
                String title = map.get("title");

                goWeb(url,title);
            }

        });
        simpleadapter1.setViewBinder(new ViewBinder() {

            @Override
            public boolean setViewValue(View view, Object data,
                    String textRepresentation) {
                if (view instanceof ImageView && data instanceof Bitmap) {
                    ImageView iv = (ImageView) view;
                    iv.setImageBitmap((Bitmap) data);
                    return true;
                }
                return false;
            }
        });


        listdata2 = new ArrayList>();
        lv2 =(ListView) adapter.getPageList().get(1).findViewById(R.id.listview_news_list2);
        btn2 = (Button)adapter.getPageList().get(1).findViewById(R.id.button1_list2);
        simpleadapter2 = new SimpleAdapter(getActivity(), listdata2, R.layout.rlist, new String[] { "title", "info",
            "img" }, new int[] { R.id.title, R.id.info, R.id.img });
        lv2.setAdapter(simpleadapter2);
        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(!listdata2isInit) {
                    **new GetDataTask2().execute();**
                    listdata2isInit = true;
                }
            }
        });
        lv2.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                // TODO Auto-generated method stub

                HashMap map = (HashMap)lv2.getItemAtPosition(position);
                String url = map.get("url");
                String title = map.get("title");

                goWeb(url,title);
            }

        });
        simpleadapter2.setViewBinder(new ViewBinder() {

            @Override
            public boolean setViewValue(View view, Object data,
                    String textRepresentation) {
                if (view instanceof ImageView && data instanceof Bitmap) {
                    ImageView iv = (ImageView) view;
                    iv.setImageBitmap((Bitmap) data);
                    return true;
                }
                return false;
            }
        });

        // ViewPager滑动监听器  
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {  

            @Override
            public void onPageSelected(int arg0) {  
                // TODO Auto-generated method stub  
                // 当滑动时,顶部的imageView是通过animation缓慢的滑动  
                switch (arg0) {  
                case 0:
                    listdata2isInit = false;
                    listdata3isInit = false;
                    break;  
                case 1:
                    listdata1isInit = false;
                    listdata3isInit = false;
                    break;  
                case 2:
                    listdata1isInit = false;
                    listdata2isInit = false;
                    break;
                default:
                    break;
                }  

            }  

            @Override  
            public void onPageScrolled(int arg0, float arg1, int arg2) {  
                // TODO Auto-generated method stub  
//              Toast.makeText(getApplicationContext(), "您正在滑动页面",Toast.LENGTH_SHORT).show();

            }  

            @Override  
            public void onPageScrollStateChanged(int arg0) {  
                // TODO Auto-generated method stub  
                if(arg0==2){
//                  Toast.makeText(getApplicationContext(), "页面滑动完毕", Toast.LENGTH_SHORT).show();
                }

            }  
        });  
    }


    void fillList1(){

        if(!listdata1isInit) {
            new GetDataTask1().execute();
            listdata1isInit = true;
        }
    }

    void fillList2(){

        if(!listdata2isInit) {
            new GetDataTask2().execute();
            listdata2isInit = true;
        }
    }

    void fillList3(){

        if(!listdata3isInit) {
            new GetDataTask3().execute();
            listdata3isInit = true;
        }
    }

    private void goWeb(String url,String title){
        Intent it1 = new Intent(getActivity().getApplicationContext(),WebView1Activity.class);
        it1.putExtra("url", url);
        it1.putExtra("title", title);
        startActivity(it1);
    }

    public class ViewPagerAdapter extends PagerAdapter{  

        List viewLists;  
        ListView lv1;

        public ViewPagerAdapter(List lists)  
        {  
            viewLists = lists;  
        }  

        @Override  
        public int getCount() {                                                                 //获得size  
            // TODO Auto-generated method stub  
            return viewLists.size();  
        }  

        @Override  
        public boolean isViewFromObject(View arg0, Object arg1) {                           
            // TODO Auto-generated method stub  
            return arg0 == arg1;  
        }  

        @Override  
        public void destroyItem(View view, int position, Object object)                       //销毁Item  
        {  
            ((ViewPager) view).removeView(viewLists.get(position));  
        }  

        @Override  
        public Object instantiateItem(View view, int position)                                //实例化Item  
        {  
            ((ViewPager) view).addView(viewLists.get(position), 0);  

            return viewLists.get(position);  
        }  

        public List getPageList(){
            return viewLists;
        }

    }

    public Bitmap getBitmap(String urlStr) {
        Bitmap mBitmap = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream is = conn.getInputStream();
            mBitmap = BitmapFactory.decodeStream(is);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return mBitmap;
    }

    public List> requestHtml(int order)
            throws IOException {
        List> list = new ArrayList>();
        Document doc = new Document("");
        Elements listDiv = new Elements();
//      Document document = Jsoup.parse(new File("file:///android_asset/html/qqnews.html"),
//              "gbk");
        switch(order){
        case 0:
        /* 建立连接并获取HTML文档*/
            **doc=Jsoup.connect("http://www.news.qq.com") 
                       .data("jquery", "java")   
                       .userAgent("Mozilla")   
                       .cookie("auth", "token")   
                       .timeout(50000)   
                       .get();**
        /*对文档进行解析,这个要自己去看新闻网站的新闻分类,以及元素组织结构*/
            **listDiv = doc.select(".Q-tpList"); /* 利用“类选择器”选择元素*/
            for (Element element : listDiv) {
                Elements aTag = element.getElementsByTag("a");
                Elements pTag = element.getElementsByTag("p");
                Element aTag1 = aTag.get(0);
                Element aTag2 = aTag.get(1);
                Element pTag1 = pTag.get(0);
                String url = aTag1.attr("href");
                Elements imgTag = aTag1.getElementsByTag("img");
                String imgUrl = imgTag.attr("src");
                String title = aTag2.text().toString();
                String info = pTag1.text().toString();
                Map map = new HashMap();
                map.put("title", title);
                map.put("img", getBitmap(imgUrl));
                map.put("url", url);
                map.put("info", info);
                list.add(map);
            }**
            break;
        case 1:
            doc=Jsoup.connect("http://news.ifeng.com/society")   
                        .data("jquery", "java")   
                        .userAgent("Mozilla")   
                        .cookie("auth", "token")   
                        .timeout(50000)   
                        .get();
            listDiv = doc.select(".juti_list");
            for (Element element : listDiv) {
                Elements aTag = element.getElementsByTag("a");
                Elements pTag = element.getElementsByTag("p");
                Element aTag1 = aTag.get(0);
                Element aTag2 = aTag.get(1);
                Element pTag1 = pTag.get(0);
                String url = aTag1.attr("href");
                Elements imgTag = aTag2.getElementsByTag("img");
                String imgUrl = imgTag.attr("src");
                String title = aTag1.text().toString();
                String info = pTag1.text().toString();

                Map map = new HashMap();
                map.put("title", title);
                map.put("img", getBitmap(imgUrl));
                map.put("url", url);
                map.put("info", info);
                list.add(map);
            }
            break;
        case 2:
            Log.i("163.com","start");
            doc=Jsoup.connect("http://www.news.163.com")
                        .data("jquery", "java")   
                        .userAgent("Mozilla")
                        .cookie("auth", "token")
                        .timeout(50000)   
                        .get();
            Log.i("doc", doc.toString());
            listDiv = doc.select("div.clearfix");
            Log.i("163.com","end");
            String listDiv_size = ""+listDiv.size();
            Log.i("listDiv_size",listDiv_size);
            for (Element element : listDiv) {
                Elements aTag = element.getElementsByTag("a");
                Elements pTag = element.getElementsByTag("p");
                Element aTag1 = aTag.get(0);
                Element aTag2 = aTag.get(1);
                Element aTag3 = aTag.get(2);
                Element pTag1 = pTag.get(0);
                String url = aTag1.attr("href");
                Elements imgTag = aTag1.getElementsByTag("img");
                String imgUrl = imgTag.attr("src");
                String title = aTag2.text().toString();
                String info = aTag3.text().toString();

                Map map = new HashMap();
                map.put("title", title);
                map.put("img", getBitmap(imgUrl));
                map.put("url", url);
                map.put("info", info);
                list.add(map);
            }
            Log.i("Jsoup-end","Jsoup-end");
            String i = ""+list.size();
            Log.i("list-size",i);
            break;
        default:
            break;
        }

        return list;
    }

    private class GetDataTask1 extends AsyncTask>> {

        @Override
        protected List> doInBackground(String... params) {
            /**
             * 请求网络,获取新闻列表数据
             */
            List> requestHtml = null;
            try {
                requestHtml = requestHtml(0);
            } catch (IOException e) {
                e.printStackTrace();
            }

            return requestHtml;
        }

        @Override
        protected void onPostExecute(List> data) {
            listdata1.addAll(data);
            simpleadapter1.notifyDataSetInvalidated();
        }

    }

    private class GetDataTask2 extends AsyncTask>> {

        @Override
        protected List> doInBackground(String... params) {
            /**
             * 请求网络,获取新闻列表数据
             */
            List> requestHtml = null;
            try {
                requestHtml = requestHtml(1);
            } catch (IOException e) {
                e.printStackTrace();
            }

            return requestHtml;
        }

        @Override
        protected void onPostExecute(List> data) {
            listdata2.addAll(data);
            simpleadapter2.notifyDataSetInvalidated();
        }

    }

    private class GetDataTask3 extends AsyncTask>> {

        @Override
        protected List> doInBackground(String... params) {
            /**
             * 请求网络,获取新闻列表数据
             */
            List> requestHtml = null;
            try {
                requestHtml = requestHtml(2);
            } catch (IOException e) {
                e.printStackTrace();
            }

            return requestHtml;
        }

        @Override
        protected void onPostExecute(List> data) {
            listdata3.addAll(data);
            simpleadapter3.notifyDataSetInvalidated();
        }

    }
}

前台布局代码:

这是对于Fragment的布局,只用了一个ViewPager:
mytab1.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager_firstpage_todaytopline"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content" />

LinearLayout>

这是ViewPager每一个Pager里面的一个列表的一个项的布局(一个列表项对应一条新闻):

rlist.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:textSize="14dp" />

        <TextView
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:textSize="11dp" />
    LinearLayout>

LinearLayout>

注意事项:

1、访问网页的代码记得写在异步任务类 AsyncTask 里面,或者自己 new
一个Thread,这是Android安全机制所考虑的,也是Android开发的常识。

2、网上说对于Jsoup包的使用,需要在工程根目录下new 一个名为 “libs”的 folder 将Jsoup包添加进去,然后 build
path ,反正我就是这样做的,读者有时间可以试一试是否必须是 这样添加包。下面给出我的工程结构图:

Android利用Jsoup解析实现简单新闻客户端_第3张图片

3、这是本人自己当时遇到的一个问题。我当时不知道什么时候导入了两个 Jsoup包,后来程序就一直出错(是运行时出错,好像提示是
无法找到类和资源什么),我还傻乎乎花时间苦想了两天~~~~。当然这不仅仅是这个新闻客户端的问题了,以后大家如果出现找不到资源类似的错误,可以看看自己是不是导错包了
(也许原因就是这么简单)

你可能感兴趣的:(Android利用Jsoup解析实现简单新闻客户端)