解析数据并在webview中加载html(Parse data and load html in a webview)

编程入门 行业动态 更新时间:2024-10-24 14:23:04
解析数据并在webview中加载html(Parse data and load html in a webview)

我有一个URL,我使用Jsoup来获取数据并删除标题。 但是当我加载到Webview时,一些图像没有显示,这就是我的问题。

我的代码在这里!

@Override protected Void doInBackground(Void... params) { try { Document doc = Jsoup.connect(URL).timeout(10 * 300) .userAgent(USER_AGENT_MOBILE).get(); doc.outputSettings().escapeMode(EscapeMode.xhtml); doc.select("div.header").remove(); String b = doc.toString(); webView.loadData(b, "text/html", "utf-8"); } catch (IOException e) { e.printStackTrace(); } return null; } }

产量

I have a URL, I used Jsoup to get data and remove header. But when I load to Webview some images not show, that is my problem.

My code here !

@Override protected Void doInBackground(Void... params) { try { Document doc = Jsoup.connect(URL).timeout(10 * 300) .userAgent(USER_AGENT_MOBILE).get(); doc.outputSettings().escapeMode(EscapeMode.xhtml); doc.select("div.header").remove(); String b = doc.toString(); webView.loadData(b, "text/html", "utf-8"); } catch (IOException e) { e.printStackTrace(); } return null; } }

Output

最满意答案

用这个

byte[] imageRaw = null; try { URL url = new URL("http://some.domain.tld/somePicture.jpg"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); int c; while ((c = in.read()) != -1) { out.write(c); } out.flush(); imageRaw = out.toByteArray(); urlConnection.disconnect(); in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT); String urlStr = "http://example.com/my.jpg"; String mimeType = "text/html"; String encoding = null; String pageData = "<img src=\"data:image/jpeg;base64," + image64 + "\" />"; WebView wv; wv = (WebView) findViewById(R.id.webview); wv.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr);

use this

byte[] imageRaw = null; try { URL url = new URL("http://some.domain.tld/somePicture.jpg"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); int c; while ((c = in.read()) != -1) { out.write(c); } out.flush(); imageRaw = out.toByteArray(); urlConnection.disconnect(); in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT); String urlStr = "http://example.com/my.jpg"; String mimeType = "text/html"; String encoding = null; String pageData = "<img src=\"data:image/jpeg;base64," + image64 + "\" />"; WebView wv; wv = (WebView) findViewById(R.id.webview); wv.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr);

更多推荐

本文发布于:2023-08-01 12:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357938.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   加载   数据   data   load

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!