您的位置:首页 > 移动开发 > Android开发

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中要设置一个默认图片,以免获取不到图片的时候显示为空。



以上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息