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

webview jsoup解析html

2016-06-15 20:26 387 查看
package com.example.parsehtml;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;

import tools.HttpUtils;
import tools.LoadImageUtils;
import tools.TouchImageView;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Html.TagHandler;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebView.HitTestResult;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class WebViewActivity2 extends Activity {
Document doc;
Map<String, String> map;
ListView listView;
String value;
WebView webview;
String v;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
//
switch (msg.what) {
case 1:
// webview.loadData(value, "text/html; charset=UTF-8",
// null);//这种写法可以正确解码
setWebView();
break;

}
}

};

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview_html);
webview = (WebView) findViewById(R.id.webView1);
webview.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
HitTestResult hitTestResult = ((WebView) v).getHitTestResult();
if (null == hitTestResult) {
System.out.println("非图片");
return false;
}else{

if (hitTestResult.getType() == HitTestResult.IMAGE_TYPE) {
try {
System.out.println("点击到了图片。。。。。。。。。。。。。");
showpopup(hitTestResult.getExtra().toString()); //图片URL
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
}

});
WebSettings webSetting = webview.getSettings();
webSetting.setBuiltInZoomControls(true); // 设置显示缩放按钮
webSetting.setSupportZoom(true); // 支持缩放

webSetting.setDefaultTextEncodingName("utf-8");
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

new Thread() {
public void run() {
load();
}
}.start();
}
});

}

protected void load() {
try {
System.out.println("loading............");
doc = Jsoup.parse(new URL(
"http://help.3g.163.com/15/1011/12/B5L87DGV00963VRO.html"),
5000);

Element text = doc.getElementById("endText");
value = text.html();
v = value.replaceAll("<img [^>]*/>", ""); // [^>]*是匹配任意个非>方挂号的字符
// 过滤掉<img />标签用
handler.sendEmptyMessage(1);
} catch (Exception e1) {
e1.printStackTrace();
}
}

/**
* 将信息显示到WebView上
*/
private void setWebView() {
String htmlContent = "";
try {
InputStream in = getAssets().open("context.html");
byte[] tmp = HttpUtils.readInputStream(in);
htmlContent = new String(tmp);
// checkParams(); // 最后校验属性,防止异常标签引发的异常
webview.loadData(
htmlContent.replace("@title", "新闻标题可定义")
.replace("@source", "新闻来源")
.replace("@pubdate", "1911-5-10")
.replace("@content", value),
"text/html;charset=utf-8", null);
} catch (Exception e) {
handler.sendEmptyMessage(2); // 出现异常
e.printStackTrace();
}
}

/**
* 做最后的校验,校验要被显示到UI上的某篇新闻具体内容的各属性是否为空
*/
/*
* private void checkParams() { this.title = (title == null ||
* title.equals("")) ? "未知" : title; this.copyright = (copyright == null ||
* copyright.equals("")) ? "未知" : copyright; this.sourse = (sourse == null
* || sourse.equals("")) ? "未知" : sourse; this.pubDate = (pubDate == null ||
* pubDate.equals("")) ? "未知" : pubDate; this.description = (description ==
* null || description.equals("")) ? "未知" : description; }
*/

public void showpopup(String url) throws Exception{

TouchImageView img = new TouchImageView(this);
img.setMaxZoom(4f);
img.setScaleType(ScaleType.FIT_XY);
LoadImageUtils.loadImageFromNet(url, img);
final PopupWindow popupWindow = new PopupWindow(img,
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);

// popupWindow.setTouchable(false);

// popupWindow.setOutsideTouchable(false);

// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
// 我觉得这里是API的一个bug
ColorDrawable dw = new ColorDrawable(-00000);
popupWindow.setBackgroundDrawable(dw);
img.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
// 设置好参数之后再show
popupWindow.showAsDropDown(img);

}

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