![](/img/trans.png)
[英]How to get the content of a DOM from a HTML loaded in an Android webview?
[英]How to get HTML Content from WebView for print?
我在webview中的代码如下:
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.mainWebView);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.setWebViewClient(null);
webView.loadUrl("javascript:window.HTMLOUT.processHTML('<div>'+document.getElementsByTagName('div')[0].innerHTML+'</div>');");
}
});
webView.loadUrl("http://sample.com/");
而且我也有用于webview打印的代码,如下所示
case R.id.Send_Button:{
String msg = webView.getUrl().toString();
if(msg.length()>0){
SendDataByte(PrinterCommand.POS_Print_Text(msg, CHINESE, 0, 0, 0, 0));
SendDataByte(Command.LF);
}else{
Toast.makeText(Main_Activity.this, getText(R.string.empty), Toast.LENGTH_SHORT).show();
}
break;
}
问题是,为什么只打印url? 而我想在http://sample.com/上打印页面?
我真的需要你的建议。 谢谢
使用需要使用jsoup来获取网页内容
URL urlobject = new URL(url);
HttpURLConnection connect = (HttpURLConnection) urlobject.openConnection();
connect.setRequestMethod("HEAD");
connect.connect();
if(connect.getResponseCode() == HttpURLConnection.HTTP_OK){
org.jsoup.nodes.Document doc;
try {
// in doc variable you get the complete data of webpage
org.jsoup.nodes.Document doc;
doc = Jsoup.connect(url).get();
Element title = doc.body();
Spanned html =
Html.fromHtml(Html.fromHtml(title.toString()).toString());
}catch (IOException e){
dialog.dismiss();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.