尝试使用Jsoup将网页的内容获取并解析出来,有关jsoup的解析请点这,点我
新闻获取之后就是使用数据适配器将新闻列显示在android客户端上,效果图为
下拉刷新和加载更多使用的是开源框架XListView。
NewsDetail;
package com.example.bean; public class NewsDetail { /** * 标题 */ private String title; /** * 作者 */ private String author; /** * 发布日期 */ private String pDate; /** * 浏览次数 */ private int amount; /** * 链接地址 */ private String link; /** * 图片链接 */ private String imageLink; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getpDate() { return pDate; } public void setpDate(String pDate) { this.pDate = pDate; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getImageLink() { return imageLink; } public void setImageLink(String imageLink) { this.imageLink = imageLink; } }链接URL工具类ConnectUtil
package com.example.util; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class ConnectUtil { /** * @param urlStr * @return * @throws Exception */ public static String doGet(String urlStr) throws Exception { StringBuffer sb = new StringBuffer(); try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setDoInput(true); conn.setDoOutput(true); if (conn.getResponseCode() == 200) { InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is,"UTF-8"); //注意这一点,容易出现中文乱码 int len = 0; char[] buf = new char[1024]; while ((len = isr.read(buf)) != -1) { sb.append(new String(buf, 0, len)); } is.close(); isr.close(); } else { throw new Exception("访问网络失败!"); } } catch (Exception e) { throw new Exception("访问网络失败!"); } return sb.toString(); } }新闻类型链接处理:
public interface URLDetail { public static final int NEWS_LIST_WYYW = 1; public static final int NEWS_LIST_YBDT = 2; public static final int NEWS_LIST_TZGG = 3; public static final int NEWS_LIST_JKYXX = 4; }
package com.example.util; public class URLUtil { /** * 蔚园要问 * 院部动态 * 通知公告 * 教科研信息 */ public static final String NEWS_LIST_WYYW = "http://www.chzu.edu.cn/s/1/t/1152/p/2/list.htm"; public static final String NEWS_LIST_YBDT = "http://www.chzu.edu.cn/s/1/t/1152/p/3/list.htm"; public static final String NEWS_LIST_TZGG = "http://www.chzu.edu.cn/s/1/t/1152/p/4/list.htm"; public static final String NEWS_LIST_JKYXX = "http://www.chzu.edu.cn/s/1/t/1152/p/5/list.htm"; public static String gengrateURL(int newsType){ String urlStr = ""; switch (newsType) { case URLDetail.NEWS_LIST_WYYW: urlStr = NEWS_LIST_WYYW; break; case URLDetail.NEWS_LIST_YBDT: urlStr = NEWS_LIST_YBDT; break; case URLDetail.NEWS_LIST_TZGG: urlStr = NEWS_LIST_TZGG; break; case URLDetail.NEWS_LIST_JKYXX: urlStr = NEWS_LIST_JKYXX; break; default: urlStr = NEWS_LIST_WYYW; break; } return urlStr; } }网页解析,得到新闻列表List,NewsDetailUtil
package com.example.util; import java.util.ArrayList; import java.util.List; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import com.example.bean.NewsDetail; public class NewsDetailUtil { public List<NewsDetail> getNewsList(int newsType) throws Exception{ String html = ConnectUtil.doGet(URLUtil.gengrateURL(newsType)); Document doc = Jsoup.parse(html, "UTF-8"); List<NewsDetail> list = new ArrayList<NewsDetail>(); NewsDetail newsDetail = null; Elements ListDiv = doc.getElementsByClass("columnStyle"); for(int i=0;i<ListDiv.size();i++){ newsDetail = new NewsDetail(); Element column_ele = ListDiv.get(i); Element td_ele = column_ele.getElementsByTag("a").get(0); newsDetail.setTitle(td_ele.text().trim()); newsDetail.setLink("http://www.chzu.edu.cn"+td_ele.attr("href")); Element time_ele = column_ele.getElementsByClass("postTime").get(0); newsDetail.setpDate(time_ele.text()); list.add(newsDetail); } return list; } }Apdate
package com.example.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.example.bean.NewsDetail; import com.example.getnews.R; public class NewsAdapter extends BaseAdapter { private LayoutInflater mInflater; private List<NewsDetail> mDatas; public NewsAdapter(Context context, List<NewsDetail> datas){ this.mDatas = datas; mInflater = LayoutInflater.from(context); } public void addAll(List<NewsDetail> mDatas){ this.mDatas.addAll(mDatas); } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView == null){ convertView = mInflater.inflate(R.layout.news_item_temp, null); holder = new ViewHolder(); holder.mTitle = (TextView) convertView.findViewById(R.id.id_title); holder.mContent = (TextView) convertView.findViewById(R.id.id_url); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } NewsDetail newsDetail = mDatas.get(position); holder.mTitle.setText(newsDetail.getTitle()); holder.mContent.setText(newsDetail.getLink()); return convertView; } private final class ViewHolder { TextView mTitle; TextView mContent; } }界面导航条TabApdate
package com.example.getnews; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class TabAdapter extends FragmentPagerAdapter{ public static final String[] TITLES = new String[] {"蔚园要问","院部动态","通知公告","教科研信息"}; public TabAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { MainFragment fragment = new MainFragment(arg0+1); return fragment; } @Override public int getCount() { return TITLES.length; } @Override public CharSequence getPageTitle(int position) { return TITLES[position % TITLES.length]; } }数据“填充”
package com.example.getnews; import java.util.ArrayList; import java.util.List; import me.maxwin.view.IXListViewLoadMore; import me.maxwin.view.IXListViewRefreshListener; import me.maxwin.view.XListView; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.adapter.NewsAdapter; import com.example.bean.NewsDetail; import com.example.util.NewsDetailUtil; import com.example.util.URLDetail; public class MainFragment extends Fragment implements IXListViewRefreshListener, IXListViewLoadMore{ private int newsType = URLDetail.NEWS_LIST_WYYW; private NewsDetailUtil newsDetailUtil; private XListView mXListView; private NewsAdapter mAdapter; private List<NewsDetail> mDatas = new ArrayList<NewsDetail>(); public MainFragment(int newsType){ this.newsType = newsType; newsDetailUtil = new NewsDetailUtil(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab_item_fragment_main, null); } @Override public void onActivityCreated( Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mAdapter = new NewsAdapter(getActivity(), mDatas); mXListView = (XListView) getView().findViewById(R.id.id_xlistView); mXListView.setAdapter(mAdapter); mXListView.setPullRefreshEnable(this); mXListView.setPullLoadEnable(this); mXListView.startRefresh(); } @Override public void onLoadMore() { } @Override public void onRefresh() { new LoadDatasTask().execute(); } class LoadDatasTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { try { List<NewsDetail> newsDetail = newsDetailUtil.getNewsList(newsType); mDatas = newsDetail; } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { mAdapter.addAll(mDatas); mAdapter.notifyDataSetChanged(); mXListView.stopRefresh(); } } }
Mainactivity
public class MainActivity extends FragmentActivity { private TabPageIndicator mIndicator; private ViewPager mViewPager; private FragmentPagerAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIndicator = (TabPageIndicator) findViewById(R.id.id_indicator); mViewPager = (ViewPager) findViewById(R.id.id_pager); mAdapter = new TabAdapter(getSupportFragmentManager()); mViewPager.setAdapter(mAdapter); mIndicator.setViewPager(mViewPager, 0); } }源码下载