Android根据网址获取标题和图片(解析Html获取标题和图片)
2017-09-26 14:20
399 查看
需求大概是随便复制一个网址,我们能解析出其标题和图片来展示
先贴上实现好的效果点击网址链接,自动把黏贴板的复制黏贴上去,如下
点击解析后呈现的效果↓。
第一步是导入一个jar包 Jsoup,该工具是封装了对html的解析,下面是下载地址
https://jsoup.org/download
导入jar包后,开始代码
首先是选择对话弹窗
private void showPhotoDialog() { final AlertDialog dlg = DialogManager.getDialog(getContext()).create(); dlg.show(); Window window = dlg.getWindow(); window.setContentView(R.layout.fx_dialog_social_main); TextView tv_paizhao = (TextView) window.findViewById(R.id.tv_content1); tv_paizhao.setText("文字,图片"); tv_paizhao.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getContext(), xxx.class); startActivity(intent); dlg.dismiss(); } }); TextView tv_xiangce = (TextView) window.findViewById(R.id.tv_content2); tv_xiangce.setText("网址链接"); tv_xiangce.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showUrlWindow();//这里就打开解析窗口 dlg.dismiss(); } }); }
private void showUrlWindow() { //获取黏贴板内容 ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE); String text = null; if( clipboard.getText()!=null) { text = clipboard.getText().toString();//这里得到系统剪切板的复制内容 } final AlertDialog dlg = DialogManager.getDialog(getContext()).create(); dlg.show(); Window window = dlg.getWindow(); window.setContentView(R.layout.window_url); dlg.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);//清除默认隐藏键盘 final EditText ed = (EditText) window.findViewById(R.id.window_ed); if(!TextUtils.isEmpty(text)){ ed.setText(text); //自动黏贴网址 } Button bt = (Button) window.findViewById(R.id.window_bt); bt.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(!TextUtils.isEmpty(ed.getText())){ dialog.show(); //以下是用了Rxjava如果不懂的可以使用thread+handler进行处理 Observable.create(new ObservableOnSubscribe<Map>() { @Override public void subscribe(ObservableEmitter<Map> emitter) throws Exception { Map map = null; try { //这里开始是做一个解析,需要在非UI线程进行 String imgStr=""; Document document = Jsoup.parse(new URL(ed.getText().toString().trim()), 5000); String title = document.head().getElementsByTag("title").text(); Elements imgs = document.getElementsByTag("img");//取得所有Img标签的值 if(imgs.size()>0){ imgStr = imgs.get(0).attr("abs:src");//默认取第一个为图片 } map=new HashMap() ; map.put("code","1"); map.put("title",title); map.put("url",ed.getText()); map.put("img",imgStr); emitter.onNext(map); } catch (IOException e) { map=new HashMap() ; map.put("code","0"); emitter.onNext(map); dialog.dismiss(); e.printStackTrace(); } } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<Map>() { @Override public void accept(Map map) throws Exception { //以下操作是在主线程中进行,也就是在handler中 dialog.dismiss(); if(map.get("code").equals("1")){ dlg.dismiss(); Intent intent = new Intent(); intent.setClass(getContext(), MomentsPublishActivity.class); intent.putExtra("title",map.get("title").toString()); intent.putExtra("url",map.get("url").toString()); intent.putExtra("img",map.get("img").toString()); startActivity(intent); }else{ Toast.makeText(getContext(),"解析网址失败,请检查是否包含http://",Toast.LENGTH_LONG).show(); } } }); } } }); }
以上或得图片的地址和tittle后,就可以在以下布局中展示了。
<LinearLayout android:id="@+id/articleLay" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/gray" android:padding="5dp" android:layout_marginBottom="10dp" android:visibility="gone" android:orientation="horizontal"> <ImageView android:id="@+id/articleImg" android:layout_width="50dp" android:layout_height="50dp" android:scaleType="centerCrop" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/articleTitle" android:layout_width="wrap_content" android:layout_height="match_parent" android:ellipsize="end" android:maxLines="2" android:gravity="center_vertical" android:textSize="15sp" android:layout_marginLeft="10dp" android:text="AAAAAAAAAAAAAAAA"/> </LinearLayout>
有些网址获取到的img标签并不是正确的图片地址,而是相对路径,所以布局中的imageview中要设置一个默认图片,以免获取不到图片的时候显示为空。
以上
相关文章推荐
- android学习笔记16--------------Android实现网络图片与html源码获取
- asp.net正则表达式提取网页网址、标题、图片,滤所有HTML标签
- asp.net正则表达式提取网页网址、标题、图片实例以及过滤所有HTML标签实例
- Android入门进阶教程(11)-远程图片获取和Xml解析
- Android中,根据图片名称获取res文件夹中的图片
- Android根据图片文件名获取它的资源ID 的两种方式
- android学习笔记16--------------Android实现网络图片与html源码获取
- Android textview获取html文字 + 图片显示
- asp.net正则表达式提取网页网址、标题、图片实例以及过滤所有HTML标签实例
- android 根据图片路径和大小获取缩图
- Android 根据drawable中图片的名称获取ID
- asp.net正则表达式提取网页网址、标题、图片实例以及过滤所有HTML标签实例
- C# 根据URL返回HTML_根据URL获取图片信息/缩略图
- Android中如何根据图片url路径来获取网络图片
- android 根据图片的URI获取对应的图片
- WP8__从windowsphone app store 中根据app id获取应用的相关信息(下载网址及图片id等)
- android_学习—获取网络数据(图片,html)
- Android获取网址前的网站Icon图片
- asp.net正则表达式提取网页网址、标题、图片,滤所有HTML标签
- asp.net正则表达式提取网页网址、标题、图片实例以及过滤所有HTML标签实例