qq新闻 内容抓取 正则表达

2019-04-15 17:12发布

  qq 国内新闻 http://news.qq.com/c/816guonei_1.htm  http://news.qq.com/c/816guonei_2.htm                                                      ........   学校老师私开小卖部 带方便面进校遭食堂老板殴打

达州市渠县三汇中学王道明老师的妻子邓女士,在谈到丈夫与学校食堂承包商刘志勇,因携带方便面进校门发生纠纷继而被刘志勇打一事时,双眼恍惚,充满了恐惧。

([^]*)

([^

]*)

  demo 如图:

    根据返回内容 进行正则匹配,获取 需要布局信息 QQNewsItemEntity  (图片展示不好,可以查看图片附件)

  package com.curiousby.fitnessandappointment.utils; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.curiousby.fitnessandappointment.entity.QQNewsItemEntity; public class QQNewsMatcherPerformer { public static List parseHtmlData(String result) { List list = new ArrayList(); Pattern pattern = Pattern .compile("([^]*)

([^

]*)

"); Matcher matcher = pattern.matcher(result); while (matcher.find()) { QQNewsItemEntity model = new QQNewsItemEntity(); model.setNewsDetailUrl(matcher.group(1).trim()); model.setUrlImgAddress(matcher.group(2).trim()); model.setNewsTitle(matcher.group(3).trim()); model.setNewsSummary(matcher.group(4).trim()); list.add(model); } return list; } }
    package com.curiousby.fitnessandappointment.entity; public class QQNewsItemEntity { //新闻详情地址 private String newsDetailUrl; //新闻图片地址 private String urlImgAddress; //新闻标题 private String newsTitle; //新闻概要 private String newsSummary; public String getUrlImgAddress() { return urlImgAddress; } public void setUrlImgAddress(String urlImgAddress) { this.urlImgAddress = urlImgAddress; } public String getNewsDetailUrl() { return newsDetailUrl; } public void setNewsDetailUrl(String newsDetailUrl) { this.newsDetailUrl = newsDetailUrl; } public String getNewsTitle() { return newsTitle; } public void setNewsTitle(String newsTitle) { this.newsTitle = newsTitle; } public String getNewsSummary() { return newsSummary; } public void setNewsSummary(String newsSummary) { this.newsSummary = newsSummary; } }     package com.curiousby.fitnessandappointment.request.http; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.json.JSONObject; import android.util.Log; import com.android.volley.Request.Method; import com.android.volley.NetworkResponse; import com.android.volley.Response; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.VolleyError; import com.android.volley.toolbox.HttpHeaderParser; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.curiousby.fitnessandappointment.MyApplication; import com.curiousby.fitnessandappointment.constants.Constants; import com.curiousby.fitnessandappointment.entity.FeedAllEntity; import com.curiousby.fitnessandappointment.entity.FeedCommentEntity; import com.curiousby.fitnessandappointment.entity.FeedZanEntity; import com.curiousby.fitnessandappointment.entity.QueryResultJson; import com.curiousby.fitnessandappointment.quote.volley.GsonRequest; import com.curiousby.fitnessandappointment.request.dbmanager.QQNewsHttpManager; import com.curiousby.fitnessandappointment.request.dbmanager.TrendHttpManager; import com.curiousby.fitnessandappointment.utils.JSONUtil; import com.curiousby.fitnessandappointment.utils.JsonParser; import com.google.gson.JsonElement; public class QQNewsHttpRequest { public static void getQQNewsDate(final int page){ String url = Constants.getQQNewsUrl(page); StringRequest request = new StringRequest(url, new Listener() { @Override public void onResponse(String response) { if (page ==1 ) QQNewsHttpManager.getQQNewsByPageFirst(response); else QQNewsHttpManager.getQQNewsByPage(response); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { QQNewsHttpManager.getQQNewsByPageError(); } }) /* { @Override protected Response parseNetworkResponse( NetworkResponse response) { String str = null; try { str = new String(response.data,"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return Response.success(str, HttpHeaderParser.parseCacheHeaders(response)); } }*/ ; Volley.newRequestQueue(MyApplication.newInstance()).add(request); } }     package com.curiousby.fitnessandappointment.activity; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import com.curiousby.fitnessandappointment.MyApplication; import com.curiousby.fitnessandappointment.R; import com.curiousby.fitnessandappointment.adpter.QQNewsAdapter; import com.curiousby.fitnessandappointment.constants.Constants; import com.curiousby.fitnessandappointment.entity.QQNewsItemEntity; import com.curiousby.fitnessandappointment.quote.xlistview.MsgListView; import com.curiousby.fitnessandappointment.quote.xlistview.MsgListView.IXListViewListener; import com.curiousby.fitnessandappointment.request.event.QQNewsStringHttpEvent; import com.curiousby.fitnessandappointment.request.event.base.RequestEvent; import com.curiousby.fitnessandappointment.request.http.QQNewsHttpRequest; import com.curiousby.fitnessandappointment.utils.QQNewsMatcherPerformer; import de.greenrobot.event.EventBus; public class QQNewsActivity extends BaseActivity implements IXListViewListener ,OnItemClickListener{ private MsgListView mListView; private List mDataList; private QQNewsAdapter mAdapter; private static int start = 1; private Context mContext; public final static String QQ_NEWS_DETAIL_URL = "qq_news_detail_url"; @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.qqnews_list); this.mContext = QQNewsActivity.this; initUtils(); initView( ); initListeners(); EventBus.getDefault().register( this ); start =1; QQNewsHttpRequest.getQQNewsDate(start); } private void initUtils() { mDataList = new ArrayList(); mAdapter = new QQNewsAdapter(mContext); mAdapter.setmDataList(mDataList); } private void initView() { mListView = (MsgListView) this.findViewById(R.id.qq_news_list); mListView.setAdapter(mAdapter); } private void initListeners() { mListView.setPullLoadEnable(true); mListView.setPullRefreshEnable(true); mListView.setXListViewListener(this); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener( this ); } @Override public void onRefresh() { start =1; mListView.setPullLoadEnable(true); mListView.setPullRefreshEnable(true); QQNewsHttpRequest.getQQNewsDate(start); } @Override public void onLoadMore() { start += 1; QQNewsHttpRequest.getQQNewsDate(start); } @Override public void onDestroy() { EventBus.getDefault().unregister( this ); super.onDestroy(); } public void onEventMainThread(RequestEvent requestEvent){ if(requestEvent instanceof QQNewsStringHttpEvent){ QQNewsStringHttpEvent event = (QQNewsStringHttpEvent) requestEvent; switch(event.status){ case HTTP_ERROR: mListView.stopRefresh(); mListView.stopLoadMore(); mListView.setPullLoadEnable(false); break; case HTTP_START: { mListView.stopRefresh(); mListView.stopLoadMore(); String result = event.data; mAdapter.clearMDataList(); List list = QQNewsMatcherPerformer.parseHtmlData(result); mAdapter.setmDataList(list); mAdapter.notifyDataSetChanged(); } break; case HTTP_SUCCESS: { mListView.stopRefresh(); mListView.stopLoadMore(); String result = event.data; List list = QQNewsMatcherPerformer.parseHtmlData(result); mAdapter.addMDataList(list); mAdapter.notifyDataSetChanged(); } break; default: break; } } } @Override public void onItemClick( AdapterView parent, View view, int position, long id) { QQNewsItemEntity item = mAdapter.getItem(position - 1 ); if (item != null) { Intent msgIntent = new Intent(); Log.e("baoyou", Constants.url_qq_news_base + item.getNewsDetailUrl()); Bundle bundle = new Bundle(); // Bundle的底层是一个HashMap     package com.curiousby.fitnessandappointment.activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.webkit.WebSettings; import android.webkit.WebSettings.LayoutAlgorithm; import android.webkit.WebView; import android.webkit.WebViewClient; import com.curiousby.fitnessandappointment.R; import com.curiousby.fitnessandappointment.constants.Constants; public class QQNewsDetailActivity extends BaseActivity { private WebView webView; private String url ="https://www.baidu.com"; @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.qqnews_list_item_detail); initUtils(); initView( ); initListeners(); } private void initListeners() { webView.loadUrl(url); showWaitingDialog(""); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); dismissWaitingDialog(); return true; } }); } private void initView() { webView = (WebView) this.findViewById(R.id.wv_qq_news_item_detail_webview); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); //支持js //settings.setPluginsEnabled(true); //支持插件 settings.setUseWideViewPort(false); //将图片调整到适合webview的大小 settings.setSupportZoom(true); //支持缩放 settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); } private void initUtils() { Intent intent = getIntent(); Bundle bundle = intent.getBundleExtra("bundle"); String path =bundle.getString(QQNewsActivity.QQ_NEWS_DETAIL_URL); url = Constants.url_qq_news_base + path; } }   package com.curiousby.fitnessandappointment.adpter; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.curiousby.fitnessandappointment.R; import com.curiousby.fitnessandappointment.entity.QQNewsItemEntity; import com.curiousby.fitnessandappointment.quote.volley.ImageCacheManager; public class QQNewsAdapter extends BaseAdapter{ private LayoutInflater mInflater; private List mDataList; private Context mContext; public QQNewsAdapter(Context mContext) { this.mContext = mContext; this.mInflater = LayoutInflater.from(mContext); } public List getmDataList() { return mDataList; } public void setmDataList(List mDataList) { if (this.mDataList == null ) { this.mDataList = new ArrayList(); } this.mDataList = mDataList; } public void addMDataList(List mDataList) { this.mDataList.addAll(mDataList); } public void clearMDataList( ) { this.mDataList.clear(); } @Override public int getCount() { if (mDataList != null && mDataList.size() > 0) return mDataList.size(); return 0; } @Override public QQNewsItemEntity getItem(int position) { if (mDataList != null && mDataList.size() > 0) return mDataList.get(position); return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if ( convertView == null || convertView.getTag() ==null ) { convertView = mInflater.inflate( R.layout.qqnews_list_item,parent, false); viewHolder = new ViewHolder(convertView); convertView.setTag( viewHolder ); }else{ viewHolder = ( ViewHolder ) convertView.getTag(); } QQNewsItemEntity qqnie = getItem(position); if (qqnie != null) { ImageCacheManager.loadImage(qqnie.getUrlImgAddress(),viewHolder.qqnewspic, getBitmapFromRes(R.drawable.ic_launcher), getBitmapFromRes(R.drawable.ic_launcher)); viewHolder.qqnewstitle.setText(qqnie.getNewsTitle()); viewHolder.qqnewscontent.setText(qqnie.getNewsSummary()); } return convertView; } class ViewHolder{ public ImageView qqnewspic; public TextView qqnewstitle; public TextView qqnewscontent; public ViewHolder(View baseView) { this.qqnewspic = (ImageView) baseView.findViewById(R.id.qq_news_image_link); this.qqnewstitle = (TextView) baseView.findViewById(R.id.qq_news_txt_title); this.qqnewscontent = (TextView) baseView.findViewById(R.id.qq_news_txt_summary); } } public Bitmap getBitmapFromRes(int resId) { Resources res = mContext.getResources(); return BitmapFactory.decodeResource(res, resId); } }             package com.curiousby.fitnessandappointment.constants; import java.util.ArrayList; import java.util.List; import com.curiousby.fitnessandappointment.R; import com.curiousby.fitnessandappointment.entity.GridEntity; public class Constants { public final static String BASEIP ="http://127.0.0.1:8080"; public final static String login_url = BASEIP + "/RepositoryPic/WebService/rest/user/login"; public final static String friendship_url = BASEIP + "/RepositoryPic/WebService/rest/friendShip/friendShips/"; public final static String url_addDeviceToTag = BASEIP + "/RepositoryPic/WebService/rest/pushService/addDevicesToTag"; public final static String url_post_sendMessageToTag =BASEIP + "/RepositoryPic/WebService/rest/pushService/sendMessageToTag"; public final static String url_feed =BASEIP + "/RepositoryPic/WebService/rest/feedAll/feedAlls/user"; public final static String url_add_feed_comment =BASEIP + "/RepositoryPic/WebService/rest/feedAll/feedCommemtAdd"; public final static String url_add_feed_zan =BASEIP + "/RepositoryPic/WebService/rest/feedAll/feedZanAdd"; public final static String url_feed_create = BASEIP + "/RepositoryPic/WebService/rest/feedAll/createFeed"; public final static String url_qq_news_base="http://news.qq.com"; public static String url_qq_news_guonei = url_qq_news_base +"/c/816guonei_?.htm"; public static String getQQNewsUrl (int page){ return url_qq_news_guonei.replace("?", page+""); } public static List getGridItem() { List list = new ArrayList(); list.add(new GridEntity(1, "动态", R.drawable.mian_trend_grid_trend, 1)); list.add(new GridEntity(2, "悦动圈", R.drawable.mian_trend_grid_yuedongquan, 2)); list.add(new GridEntity(3, "新闻", R.drawable.qq_ketang, 3)); return list; } }                          

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。

 
 
 谢谢您的赞助,我会做的更好!